jQuery,如果任何"one"值发生变化

iNk*_*iNk 2 javascript jquery jquery-selectors

我有一段代码,当一组表单值发生变化时,这个人写了三十个函数.

我想把它们组成一个单一的功能

$('#jobs_held').change(function() {
// do something, with a little variation
});

$('#residence_years').change(function() {
// do something, with a little variation
});

$('#how_long').change(function() {
// do something, with a little variation
});
Run Code Online (Sandbox Code Playgroud)

无论如何我可以做点什么......

$('#jobs_held', '#residence_years', '#how_long').change(function(){
// do something, with a little variation
});
Run Code Online (Sandbox Code Playgroud)

use*_*716 6

将它们加入单个字符串以使用multiple-selector[docs].

$('#jobs_held,#residence_years,#how_long').change(function(){
    // do something, with a little variation

    // "this" can be used to refer to the specific element 
    //     that received the event.

    alert( this.id ); // to alert the ID of the element
});
Run Code Online (Sandbox Code Playgroud)

......或者找一些像班级这样的共同身份来全部选择它们.