本文介绍了按下Enter键时触发功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的搜索功能,我想在按Enter键时触发该功能,尽管该功能执行了,但表单也被发布了.
I've a simple search function which i Want to trigger on Enter key press, though the function executes but the form also get posted.
<script>
function search()
{
...
}
$(document).ready(function() {
$("#text").keypress(function (e) {
if (e.which==13)
search();
});
});
<body>
<form id="searchForm" name="searchForm">
<input size="40" id="text" type="text" name="text" class="input" />
</form>
</body>
推荐答案
将函数绑定到Submit事件并防止使用默认值:
Bind the function to the submit event and prevent the default:
$('form').submit(function(ev){
ev.preventDefault();
/* your code here */
});
这篇关于按下Enter键时触发功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!