[英]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;
}
}
此代碼適用於 IE7,但不適用於 Mozilla Firefox。 請幫助我在按下Enter鍵時引發按鈕事件。
Firefox假定,如果您在任何文本字段中按Enter鍵,則您要提交表單-即使這些字段不是表單的一部分,並且按鈕不是“提交”類型。
您必須使用preventDefault()覆蓋Firefox的默認行為。 在jQuery選擇器中,將div包含要忽略回車鍵的文本框-在本例中為“ page” div。 除了選擇整個div,您還可以指定要忽略的文本框。
$('#page').keypress(function(e) {
if(e.which == 13) { // Checks for the enter key
e.preventDefault(); // Stops IE from triggering the button to be clicked
}
});
我不知道是否有人再檢查此線程,但以供將來參考。
我在 FF 中遇到了同樣的問題,我在這里得到了答案:
http://www.webdeveloper.com/forum/showthread.php?t=108382
祝你好運!
你有一個錯字。
window.event
應該是window.Event
!
你也可以試試這個代碼:
theButton.click();
采用:
__doPostBack('ctl00$ContentPlaceHolder1$btnGo','');
代替:
document.getElementById("ctl00_ContentPlaceHolder1_btnGo").focus();
您可以簡單地將UseSubmitBehavior="true"屬性值添加到應該在Enter鍵上觸發的按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.