最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - binding on change event to group of input fields in jquery - Stack Overflow

programmeradmin4浏览0评论

I have three input fields as below:

 <input type="text" name="address" id="address"/>
 <input type="text" name="city" id="city"/>
 <input type="text" name="country" id="country"/>

How can i add onChange event to all of these fields at once something like this:

$("select the elements with id address,city,country").bind("change", function() {
            //do something
        });

I have three input fields as below:

 <input type="text" name="address" id="address"/>
 <input type="text" name="city" id="city"/>
 <input type="text" name="country" id="country"/>

How can i add onChange event to all of these fields at once something like this:

$("select the elements with id address,city,country").bind("change", function() {
            //do something
        });
Share Improve this question asked Jan 17, 2013 at 8:52 sonamsonam 3,76012 gold badges47 silver badges68 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 11

use , in id selector as @Rory said

OR

add a class to all this and call change function

<input type="text" name="address" id="address" class="className"/>
<input type="text" name="city" id="city" class="className"/>
<input type="text" name="country" id="country" class="className"/>

$('.className').bind("change", function(){
  //your stuff
});

HOWEVER

since it is an input field.. i recommend you to use..keyup(), using change you have to click out of the text box to make it fire.

$('.className').keyup(function(){
  //your stuff
});

You can use , as you would in CSS to combine selectors. Try this:

$("#address, #city, #country").bind("change", function() {
    //do something
});

Alternatively you can add a class to each element and select that.

发布评论

评论列表(0)

  1. 暂无评论