admin管理员组

文章数量:1426051

i am using a form and i have to use button and type is submit when i pressed enter key the form submit, so i want to remove the functionality of enter key.

<input name="bb" id="bb" type="text" onKeyUp="Javascript: if(event.keycode==13) dont_submit(key); else show_error(this.value, event.keycode, this.id)";>


<script type="text/javascript">
function dont_submit(key)
{
    if(key)
    {
    return false;
    }
}

</script>

i also try this but not worked

<script type="text/javascript">
function dont_submit(key)
{

    if(key == 13)
    {
    return false;
    }

}
</script>

i am using a form and i have to use button and type is submit when i pressed enter key the form submit, so i want to remove the functionality of enter key.

<input name="bb" id="bb" type="text" onKeyUp="Javascript: if(event.keycode==13) dont_submit(key); else show_error(this.value, event.keycode, this.id)";>


<script type="text/javascript">
function dont_submit(key)
{
    if(key)
    {
    return false;
    }
}

</script>

i also try this but not worked

<script type="text/javascript">
function dont_submit(key)
{

    if(key == 13)
    {
    return false;
    }

}
</script>
Share Improve this question edited Sep 11, 2013 at 12:30 Sasidharan 3,7503 gold badges20 silver badges37 bronze badges asked Sep 11, 2013 at 12:24 Ahad AhmedAhad Ahmed 491 silver badge7 bronze badges 1
  • 1 Don't use Javascript: in event handlers! Where do people learn this? – epascarello Commented Sep 11, 2013 at 12:53
Add a ment  | 

4 Answers 4

Reset to default 3

Try this

$('#bb').on('keydown', function (e) {
    if (e.keyCode == 13) {
        return false;
    }
});

DEMO

$(document).keypress(function (e) {
  if(e.which == 13) 
  return false;
});

Simply don't put a submit button but rather plain button with JS to submit the form:

<input type="button" class="SubmitButton" value="Submit" onclick="this.form.submit();" />

If you're concerned about visitors without JavaScript, use such code:

<noscript>
    <style type="text/css">
        .SubmitButton { display: none; }
    </style>
    <input type="submit" value="Submit" />
</noscript>
$('#button').keypress(function () { 

      if(e.which == 13) {

        return false;
      }
});

本文标签: javascripthow to remove the enter key functionalityStack Overflow