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

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

programmeradmin0浏览0评论

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 doesn't require the form IDs?

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 doesn't require the form IDs?

Share Improve this question edited Jul 10, 2010 at 20:53 Crescent Fresh 117k27 gold badges157 silver badges140 bronze badges asked Jul 10, 2010 at 20:46 AnApprenticeAnApprentice 111k201 gold badges636 silver badges1k bronze badges 1
  • Are you using (at least) jQuery 1.4.2? – Crescent Fresh Commented Jul 10, 2010 at 20:48
Add a comment  | 

2 Answers 2

Reset to default 14

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)

  1. 暂无评论