jQuery:如何在按下Enter键时触发点击事件 [英] Jquery: how to trigger click event on pressing enter key
本文介绍了jQuery:如何在按下Enter键时触发点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在按下Enter键后执行一个按钮单击事件.
I need to execute a button click event upon pressing key enter.
该事件目前尚未触发.
请在语法上帮助我.
$(document).on("click", "input[name='butAssignProd']", function () {
//all the action
});
这是我尝试在Enter上触发点击事件.
this is my attempt to fire click event on enter.
$("#txtSearchProdAssign").keydown(function (e) {
if (e.keyCode == 13) {
$('input[name = butAssignProd]').click();
}
});
推荐答案
尝试一下....
$('#txtSearchProdAssign').keypress(function (e) {
var key = e.which;
if(key == 13) // the enter key code
{
$('input[name = butAssignProd]').click();
return false;
}
});
$(function() {
$('input[name="butAssignProd"]').click(function() {
alert('Hello...!');
});
//press enter on text area..
$('#txtSearchProdAssign').keypress(function(e) {
var key = e.which;
if (key == 13) // the enter key code
{
$('input[name = butAssignProd]').click();
return false;
}
});
});
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<textarea id="txtSearchProdAssign"></textarea>
<input type="text" name="butAssignProd" placeholder="click here">
</body>
</html>
这篇关于jQuery:如何在按下Enter键时触发点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文