admin管理员组文章数量:1277297
I have an anchor tag like
<a class="btn btn-danger" id="clicking" data-bind="click: $root.enterLocation" href="#">Continue</a>
It's inside a pop-up. I need to click on this link on pressing enter key. I have tried the following code but it did not work for me.
$(document).ready(function(){
$(document).keyup(function(event){
if (event.keyCode == 13){
$("#clicking").trigger('click');
}
})
});
Not sure why the functionality is not working. I have used click function also with same result.Its working correctly on mouse click. I need to make it work automatically on enter press.
Following code is working fine in Firefox.
$(document).ready(function () {
$(document).on("keyup", function (event) {
if (event.which == 13) {
document.getElementById("clicking").click();
}
});
});
How to make this work on Chrome?
I have an anchor tag like
<a class="btn btn-danger" id="clicking" data-bind="click: $root.enterLocation" href="#">Continue</a>
It's inside a pop-up. I need to click on this link on pressing enter key. I have tried the following code but it did not work for me.
$(document).ready(function(){
$(document).keyup(function(event){
if (event.keyCode == 13){
$("#clicking").trigger('click');
}
})
});
Not sure why the functionality is not working. I have used click function also with same result.Its working correctly on mouse click. I need to make it work automatically on enter press.
Following code is working fine in Firefox.
$(document).ready(function () {
$(document).on("keyup", function (event) {
if (event.which == 13) {
document.getElementById("clicking").click();
}
});
});
How to make this work on Chrome?
Share Improve this question edited Feb 7, 2019 at 18:00 brooksrelyt 4,0356 gold badges36 silver badges57 bronze badges asked Dec 10, 2012 at 9:10 Sanjib KarmakarSanjib Karmakar 3572 gold badges5 silver badges14 bronze badges 4-
Shouldn't you use
event.which
, notkeyCode
? – Ian Commented Dec 10, 2012 at 9:19 - Works just fine for me: jsfiddle/dc9YM Just to clarify, the link works when you click on it normally with the mouse, but not when you press enter? Are you sure the page has focus when you press enter? – JJJ Commented Dec 10, 2012 at 9:22
-
@Juhana You're probably using a browser that uses
keyCode
, so that's fine, but what aboutcharCode
? Save the hassle of using either and just use the normalized propertyevent.which
that jQuery adds to provide a general, correct keycode for the event. – Ian Commented Dec 10, 2012 at 9:31 - @juhana i have a textfield on dat popup which i need to focus fast. On enter press i need to click on that link... – Sanjib Karmakar Commented Dec 10, 2012 at 9:44
3 Answers
Reset to default 3I think the problem is that you're using event.keyCode
, which isn't always used in all browsers. Some browsers use event.charCode
or even a different event.which
, which may be supported by what you're using. Anyways, the normal way to get the keycode from the event with jQuery is to use event.which
.
jQuery normalizes the event
object passed to the event handler and fixes "problems" like this so that you don't have to worry. At the same time, it seems that it copies over some of the original event's properties ("Most properties from the original event are copied over and normalized to the new event object." - from the jQuery API docs). That's probably why it's "working" for the other people menting/answering. The event
parameter passed to the handler has been generated/normalized by jQuery and will have everything you need, using the right properties. The correct way though, is to use event.which
to get a normalized keycode for the event. http://api.jquery./event.which/
$(document).ready(function () {
$(document).on("keyup", function (event) {
if (event.which == 13) {
$("#clicking").trigger('click');
}
});
});
I've created this JSFiddle: http://jsfiddle/egzsf/ It works perfectly I only added a fallback for Internet Explorer.
What does your popup looks like? Maybe it's an iFrame, that would be a logical explaination.
Code:
<a class="btn btn-danger" id="clicking" data-bind="click: $root.enterLocation" onclick="alert('test')" href="#">Continue</a>
$(document).ready(function(){
$(document).keyup(function(e){
if (e.keyCode == 13){
$("#clicking").trigger('click');
}
})
});
Better alternative is using e.which
I hope following code can help you .try it Code:
<a class="btn btn-danger" id="clicking" onclick="window.location='index.php'" href="#">Continue</a>
$(function(){
$('body').keyup(function(e){
if (e.keyCode == 13){
$("#clicking").click();
}
})
});
本文标签: javascriptClick anchor tag link on enter pressStack Overflow
版权声明:本文标题:javascript - Click anchor tag link on enter press - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741204138a2357814.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论