开发者

jQuery - call a function anytime a user changes any form input?

开发者 https://www.devze.com 2023-01-07 04:00 出处:网络
I have a page that has multiple forms. Anytime the user clicks an input or modifies text in an input I would like a function to be called. Any ideas on how to do this efficiently and in a way where it

I have a page that has multiple forms. Anytime the user clicks an input or modifies text in an input I would like a function to be called. Any ideas on how to do this efficiently and in a way where it doe开发者_StackOverflowsn't require the form IDs?


JavaScript events bubble up. So how about:

$('form').change(function() {
    // do something
}).click(function() {
    // do something
});

In each case you can query for the element that triggered the event and do what you please.


$('form input').each(function() {
     var val = this.value;
     $(this).click(function() { }
     $(this).blur(function() {
     }

});

You can also use delegate for better performance. It would help seeing your source and your exact needs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号