选择国家/地区时更改颜色(change color when a country selected)

编程入门 行业动态 更新时间:2024-10-25 04:15:06
选择国家/地区时更改颜色(change color when a country selected)

我想在<select>选项上<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"> 
  
 

I am trying to change color of a telephone field when a country is selected on the <select> options.

For example if someone selects UK the color must change to Red.

But I'm not sure what I'm doing wrong. This doesn't change at the moment.

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声明之后你有一个无用的parens。 这段代码工作正常:

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

jsFiddle演示

You had an extraneous parens after the IF statement. This code works fine:

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

jsFiddle Demo

更多推荐

本文发布于:2023-08-02 18:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379541.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:颜色   区时   国家   change   country

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!