0

假设我们有以下 div:

<div id="section">
    <input type="text" id="lastname" />
    <input type="text" id="firstname" />
    <input type="text" id="address" />
</div>

我们有以下javascript代码:

$("#section").on('change', function() {
    //get element that its value changed - code to write here
});

如果我们更改这些输入字段之一中的值,jQuery 将触发更改事件。

  1. 是否可以获得触发div上的事件的元素?

  2. 如果没有,是否有任何替代方法可以识别哪个元素触发更改事件,而无需为每个元素编写 javascript 代码?因为在一个包含许多元素的形式中会很不方便。

4

2 回答 2

1

尝试这个

现场演示

$("#section").on('change', "input", function(event) {
    alert(evt.target.id);
    //get element that its value changed - code to write here
});
于 2012-09-27T14:43:21.200 回答
0

这个怎么样(如果你能原谅双关语)?

$("#section").on('change',"input",function(){
    var something = this; //this should refer to your element.
});
于 2012-09-27T14:47:58.800 回答