jquery combine event functions

2020-02-23 06:15发布

instead of example:

$(".myfield").blur(function() {
    // validate
})
$(".myfield").keyup(function() {
    // validate
})

is there a way to combine these two?

3条回答
我欲成王,谁敢阻挡
2楼-- · 2020-02-23 06:55

use with .on() Event

$(document).on("keyup blur", "#myfield", function(event)
    {       
    // your code        

    });
查看更多
你好瞎i
3楼-- · 2020-02-23 07:01

In case you want to validate each for itself...

$('.myfield').live('blur keyup', function(event) {
  if (event.type == 'blur') {
    // validate on blur
  }
  if (event.type == 'keyup') {
    // validate on keyup
  }
});
查看更多
家丑人穷心不美
4楼-- · 2020-02-23 07:17

Yes

$(".myfield").bind('blur keyup', function(){
  // validate
});

Reference: .bind()

查看更多
登录 后发表回答