绑定不同目标的click和keyup来做同样的功能 [英] bind click and keyup of different target to do same function

查看:132
本文介绍了绑定不同目标的click和keyup来做同样的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个提交按钮,我使用 $(#submit)来执行myAction功能,但同时我也想要用户按下输入,它执行myAction功能..

I have a submit button, I use $("#submit") to perform "myAction function", but in the same time I also want if the user pressed enter, it perform "myAction function"..

我不能这样做

$("#submit").on('click keyup', function(){
 //myAction function
});

因为我必须将keyup事件附加到输入字段而不是#submit ..

because I have to attach the keyup event to my input field instead of #submit..

推荐答案

为您的函数命名并在选择器上绑定两个事件。然后添加一个特殊条件:

Give a name to your function and bind both event on the selector. Then add a special condition:

function send(e){
    if(e.type == 'click' || (e.type == 'keyup' && e.wich == 13))
}

$('[type=text]').on('keyup', send);
$('[type=submit]').on('click', send);

这篇关于绑定不同目标的click和keyup来做同样的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆