开发者

jquery combine event functions

开发者 https://www.devze.com 2023-01-08 01:56 出处:网络
instead of example: $(\".myfield\").blur(fun开发者_开发知识库ction() { // validate }) $(\".myfield\").keyup(function() {

instead of example:

$(".myfield").blur(fun开发者_开发知识库ction() {
    // validate
})
$(".myfield").keyup(function() {
    // validate
})

is there a way to combine these two?


Yes

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

Reference: .bind()


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
  }
});


use with .on() Event

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

    });
0

精彩评论

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