简体   繁体   中英

Chrome not supporting for “Enter” key onkey press event

in chrome browser i am getting TypeError: translate is not a function: The code is working in firefox correctly. but not in Chrome

Below is my code snippet:

Thanks in advance

<script language="javascript">
function translate(event)
{
  if ( event.keyCode == 13) 
  {
    submitForm('', 'mainForm');
  }
}
</script>

<form name="mainForm">
 <input id = "password"  maxlength="50" type = "password" 
                                                                   name = "password" width = "250px" onkeypress="javascript: translate(event)" />
</form>

 <script language="JavaScript">
    buttonGen("<%=mm.getMessage("IBE","IBE_PRMT_SIGN_IN_G" )%>", "javascript:submitForm('', 'mainForm')");
</script>

Try this:

$('password').keydown( function(e) {
    var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    if(key == 13) {
        e.preventDefault();
        submitForm('', 'mainForm');
    }
});

JSFIDDLE DEMO

or you can use event.which along with event.keyCode

function translate(event)
{
  if ( event.which == 13 || event.keyCode == 13) 
  {
    submitForm('', 'mainForm');
  }
}

First rename the function, you create a global function that is look like is reserved by the browser and the second when you use onkeypress attribute you don't receive a event as parameter but you can use global event variable so your function has to look like

function myTranslate(){
    if ( event.keyCode == 13){
        submitForm('', 'mainForm');
    }
}

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