简体   繁体   English

如何在JQuery中触发选择的事件(按键或模糊)

[英]How to Fire event of choice (keypress or blur) in JQuery

$("#var").keypress(function (a) {
        //line 1
        //line 2
        if(condition) {
            functionToBecalled(params);
        }

    }).blur(function (a) {
        //line 1
        //line 2
        if(condition) {
            functionToBecalled(params);
        }
    });

#var is the text-field name. #var是文本字段名称。 The function "functionToBecalled()" performs get operation using XHR in another file.Same statements are to be performed in both cases. 函数“ functionToBecall()”使用另一个文件中的XHR执行get操作。在两种情况下都将执行相同的语句。

I want the data to be retrieved as the user presses enter or clicks outside the text-field. 我希望在用户在文本字段外按Enter或单击时检索数据。

You have a mistake in your code: 您的代码有误:

... }.blur( ...

should be 应该

... }).blur( ...

since .keypress( ... ) is a function and you are chaining .blur after .keypress. 因为.keypress(...)是一个函数,并且您在.keypress之后链接.blur。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM