使用jQuery按下输入键时单击链接 [英] Clicking a link when enter key is pressed using jQuery
本文介绍了使用jQuery按下输入键时单击链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当用户在文本框中,我想尝试使用它,并按回车键,它与点击链接相同,在这种情况下,应该将它们带到另一个页面。这是我有的,它不工作。
I'm trying to make it so when a user is in a text box and they press enter, it is the same as clicking the link, in which case it should take them to another page. Here's what I have and it doesn't work.
//jQuery
$(document).ready(function() {
//if focus is in the input box drivingSchoolInput
$("#drivingSchoolInput").live("click", function() {
//if enter key is pressed
if(e.keyCode == 13) {
//click the button and go to next page
$("#button1").click();
}
});
});
标记
The markup
<form>
<div class="formDiv">
<label for="City">Search by Driving School</label>
<span class="inputBox"><input type="text" name="City" class="input" id="drivingSchoolInput" /></span>
</div>
<h4 class="submitButton"><a href="school.html" id="button1">Submit</a></h4>
</form>
推荐答案
编写一个小型jQuery插件:
Write a small jQuery plugin:
jQuery.fn.enter = function(callback) {
if(!callback) {
//don't attach if we have garbage.
return;
}
$(this).keydown(function(e) {
var ev = e || event;
if(ev.keyCode == 13) {
callback();
return false;
}
});
};
用法: $(element).enter(callback_func);
我希望这有助于。
这篇关于使用jQuery按下输入键时单击链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文