如何使用jQuery检测表单输入字段中的任何更改?

7
我正在使用以下代码片段来检测特定表单输入字段每次更改的情况。
$( '#textbox' ).on( 'input', function() {
    // Do something here
});

这个输入框属于一个表单,里面有复选框、单选按钮和更多文本输入框。有没有办法检测到任何表单字段的更改?


1
事件会冒泡,因此您可以将事件处理程序绑定到“form”元素:$( 'form' ).on( 'input', function(e) { console.log(e.target); }); - Ram
'e'function(e) 中的作用是什么? - henrywright
e 是事件对象,它的 target 属性指向事件的目标。 - Ram
谢谢解释! - henrywright
3个回答

14

试试看,

$('#Form input').on( 'input', function() {
  //This would be called if any of the input element has got a change inside the form
}); 

当页面上没有“表单”时,有没有任何方法可以实现相同的解决方案? - KWallace
@KirbyL.Wallace 那个输入元素是由什么控制的? - Rajaprabhu Aravindasamy
如果我向文本框添加了一些文本,然后将其删除或清除,这会被视为更改还是没有更改发生。如何实现好像没有更改发生的效果。 - indianwebdevil

9
请尝试以下方法:
HTML代码:
<form>
    <p><input type='text' /></p>
    <p><input type='text' /></p>
    <p><input type='checkbox' /></p>
</form>

JQuery:

$('form :input').change(function(){
   alert("Form changed");
});

JSFiddle演示


3
$("form :input").change(function() {

});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接