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

javascript - How to detect a change in any of a form's input fields using jQuery? - Stack Overflow

programmeradmin1浏览0评论

I'm using the following snippet to detect every time a particular form input field changes.

$( '#textbox' ).on( 'input', function() {
    // Do something here
});

This particular input field belongs to a form which has checkboxes, radio buttons and more text input fields. Is there a way to detect when there is a change to any of the form's fields?

I'm using the following snippet to detect every time a particular form input field changes.

$( '#textbox' ).on( 'input', function() {
    // Do something here
});

This particular input field belongs to a form which has checkboxes, radio buttons and more text input fields. Is there a way to detect when there is a change to any of the form's fields?

Share Improve this question asked Jul 15, 2014 at 13:36 henrywrighthenrywright 10.2k25 gold badges96 silver badges153 bronze badges 3
  • 1 Events bubble up, so you can bind the event handler to the form element: $( 'form' ).on( 'input', function(e) { console.log(e.target); }); – Ram Commented Jul 15, 2014 at 13:39
  • What does the 'e' do in function(e)? – henrywright Commented Jul 15, 2014 at 13:46
  • e is the event object, it's target property refers to the target of the event. – Ram Commented Jul 15, 2014 at 13:50
Add a comment  | 

3 Answers 3

Reset to default 14

Try,

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

Try this:

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 Demo

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

});
发布评论

评论列表(0)

  1. 暂无评论