【问题标题】:change color when a country selected选择国家时改变颜色
【发布时间】:2015-03-03 22:52:03
【问题描述】:

当在<select> 选项中选择国家/地区时,我正在尝试更改电话字段的颜色。

例如,如果有人选择英国,则颜色必须更改为红色。

但我不确定我做错了什么。目前没有改变。

jQuery('select[name="address[_item1][country_id]"]').change(function() {
  if (jQuery(this).val() == 'GB') {
    jQuery('#_item1telephone').css("background-color", "yellow");
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select class=" required-entry countries required-entry select" name="address[_item1][country_id]" id="_item1country_id">
  <option value=""></option>
  <option value="GB">Uruguay</option>
</select>
<input type="text" class=" required-entry input-text required-entry" value="" name="address[_item1][telephone]" id="_item1telephone">

【问题讨论】:

  • 您的 if 结尾 }) 中有一个额外的 )jsfiddle.net/j08691/vxrwLovm
  • 语法错误...删除IF 语句中多余的“)”
  • 语法修复后工作正常。 jsfiddle.net/isherwood/vx5ume5t
  • 这里的教训是你的第一站应该是浏览器控制台。那里写得很清楚。
  • @sdcr - 为什么会导致问题?

标签: javascript jquery css jquery-selectors


【解决方案1】:

在 IF 语句之后有一个多余的括号。此代码工作正常:

jQuery('select[name="address[_item1][country_id]"]').change(function() {
  if (jQuery(this).val() == 'GB') {
    jQuery('#_item1telephone').css("background-color", "yellow");
  } // <== here
});

jsFiddle Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    相关资源
    最近更新 更多