admin管理员组文章数量:1180493
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.
The code
//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>
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.
The code
//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>
Share
Improve this question
edited Jul 18, 2022 at 18:24
Brian Tompsett - 汤莱恩
5,88372 gold badges61 silver badges133 bronze badges
asked Nov 30, 2009 at 2:04
TomTom
1211 gold badge1 silver badge3 bronze badges
0
7 Answers
Reset to default 15Write 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;
}
});
};
Usage: $(element).enter(callback_func);
I hope this helps.
Check this out: jQuery Event Keypress: Which key was pressed?
I'll just consolidate the codes from that post here:
$('#searchbox input').bind('keypress', function(e) {
var code = e.keyCode || e.which;
if(code == 13) { //Enter keycode
//Do your stuff + form submit
}
});
PS: I have never tested it, but it 'should' work. :P
You have a few major problems with this code...
The first one, pygorex1 caught: you need to specify the event argument if you wish to refer to it...
The second one is in the same area of your code: you're trying to check for the key event in a handler for the click
event!
The third one can be found on this line:
//click the button and go to next page
$("#button1").click();
...which does nothing, since you have no event handlers on that link, and jQuery's click()
function does not trigger the browser's default behavior!
Instead, try something like this:
// if a key is pressed and then released
$("#drivingSchoolInput").live("keyup", function(e) {
// ...and it was the enter key...
if(e.keyCode == 13) {
// ...navigate to the associated URL.
document.location = $("#button1").attr('href');
}
});
I wanted to do something similar, so after reading David K Egghead's suggestion above I came up with this which triggers a "click" even on an anchor when enter is pressed. Could be used to "click" a button as well!
$(document).keypress(function(event){if(event.keyCode==13){$('.save_icon').trigger("click");}});
This line needs the e
:
$("#drivingSchoolInput").live("click", function(e) {
I had a very similar question. However, I was unable to find a complete solution.
I simply wanted to attach the same function to 2 different elements, with 2 different methods of executing it. For example, what Tom wanted: CLICK a link, OR press ENTER in an input field, and execute the same function (ie navigate to another page).
Jacob Relkin has presented a good idea for the "enter" keypress. I have included this in the code below:
So based on the following simple markup:
<div>
<label for="myInput">Type Stuff</label>
<input id="myInput" type="text" value="" />
<a id="myButton" href="http://google.com">Click me!</a>
</div>
Use this JavaScript (JQuery) code:
//this is the plugin Jacob Relkin suggested
(function($) {
$.fn.enter=function(callback){
this.keyup(function(e) {
var ev = e || event;
if(ev.keyCode == 13) {
callback();
return false;
}
});
};
})(jQuery);
$(document).ready(function(){
var fnDoStuff = function() {
//insert code here
//for this example I will complete Tom's task
document.location.href = $("#myButton").attr('href');
};
$('#myInput').enter(fnDoStuff);
$('#myButton').click(fnDoStuff);
});
In this way, you could attach the same function to any number of elements, with any type of interaction.
I used the following with asp.net LinkButton
$('#drivingSchoolInput').keypress(function (event) {
if (event.keyCode == 13) {
eval($('#button1').attr('href'));
}
});
I also had a FORM on the header with allowed for seaching, so I need to add another step to disable the FORM from posting.
$('#drivingSchoolInput').keypress(function (event) {
if (event.keyCode == 13) {
$('#header1_ButtonTerm').attr('disabled', 'disabled'); <-- hack added to kill FORM POST
eval($('#button1').attr('href'));
}
});
本文标签: javascriptClicking a link when enter key is pressed using jQueryStack Overflow
版权声明:本文标题:javascript - Clicking a link when enter key is pressed using jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738199007a2068303.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论