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, not keyCode? – 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 about charCode? Save the hassle of using either and just use the normalized property event.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
Add a ment  | 

3 Answers 3

Reset to default 3

I 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