Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

jquery - jQuery多个事件触发相同的功能(jQuery multiple events to trigger the same function)

Is there a way to have keyup , keypress , blur , and change events call the same function in one line or do I have to do them separately?

(有没有办法让keyupkeypressblurchange事件在一行中调用相同的函数,还是必须单独执行?)

The problem I have is that I need to validate some data with a db lookup and would like to make sure validation is not missed in any case, whether it is typed or pasted into the box.

(我遇到的问题是我需要使用数据库查找验证一些数据,并且希望确保在任何情况下都不会错过验证,无论是键入还是粘贴到框中。)

  ask by shaneburgess translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use .on() to bind a function to multiple events:

(您可以使用.on()将函数绑定到多个事件:)

$('#element').on('keyup keypress blur change', function(e) {
    // e.type is the type of event fired
});

Or just pass the function as the parameter to normal event functions:

(或者只是将函数作为参数传递给普通事件函数:)

var myFunction = function() {
   ...
}

$('#element')
    .keyup(myFunction)
    .keypress(myFunction)
    .blur(myFunction)
    .change(myFunction)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...