简体   繁体   中英

Trigger button event when press enter key on Mozilla Firefox

function CaptureKeys (e,btn) {
  var c //= document.layers ? evt.which: document.all ? event.keyCode : evt.keyCode 
  if(window.event) { 
    c = e.keyCode; // IE
  } else if(e.which) {
    c = e.which; // Netscape/Firefox/Opera
  }
      if (c == 13) {
        //return /enter key
        if (btn=="go") {
            if (document.getElementById("ctl00_ContentPlaceHolder1_btnGo")!=null) {
                document.getElementById("ctl00_ContentPlaceHolder1_btnGo").focus();
                return true;
            }
        } else {
            if (document.getElementById('ctl00_ContentPlaceHolder1_ImgFilter') != null) {
            //__doPostBack('ctl00_ContentPlaceHolder1_ImgFilter','');
            document.getElementById('ctl00_ContentPlaceHolder1_ImgFilter').focus();
            return true;
           }
       }
      return false;
     }
 }

This code is working on IE7 but not working on Mozilla Firefox. Please help me to raise button event when press Enter key.

Firefox assumes that if you press the enter key in any of the text fields, you want to submit the form -- even if the fields are not part of a form and even if the button is not of type "submit".

You must override Firefox default behavior with preventDefault(). In your jQuery selector, put in the div that contains the text boxes you want to ignore the enter key -- in your case, the "page" div. Instead of selecting the whole div, you could also specify the text boxes you want to ignore specifically.

$('#page').keypress(function(e) {
    if(e.which == 13) { // Checks for the enter key
        e.preventDefault(); // Stops IE from triggering the button to be clicked
    }
});

I dont know if anyone is checking this thread anymore, but for future reference.

I had the same problem in FF and I got the answer here:

http://www.webdeveloper.com/forum/showthread.php?t=108382

Good luck!

You have a typo.

window.event should be window.Event !

你也可以试试这个代码:

theButton.click();

Use:

 __doPostBack('ctl00$ContentPlaceHolder1$btnGo',''); 

instead of:

document.getElementById("ctl00_ContentPlaceHolder1_btnGo").focus(); 

您可以简单地将UseSubmitBehavior="true"属性值添加到应该在Enter键上触发的按钮。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM