选择国家时更改颜色

时间:2015-03-03 14:39:47

标签: javascript jquery css jquery-selectors

我想在<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">

1 个答案:

答案 0 :(得分:0)

在IF声明之后你有一个无用的parens。这段代码工作正常:

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

jsFiddle Demo

相关问题