How to disable auto submit behavior when hitting enter key?

前端 未结 7 1279
故里飘歌
故里飘歌 2020-12-17 16:52

I want to hit enter key to go to p2.htm or p3.htm according to the input text that I was typing in. And I also want to hit submit1 button to alert(\'no1\') manually.

<
相关标签:
7条回答
  • 2020-12-17 17:17

    This function should do the trick:

    function submitOnEnter(e, page) {
        var key;
    
        if (window.event)
            key = window.event.keyCode; //IE
        else
            key = e.which; //Firefox & others
    
        if(key == 13)
            window.location = page;
    }
    

    You should call it like this:

    <input id="Text2"  type="text"  onkeyup="submitOnEnter(event, 'p2.html')" /> 
    
    0 讨论(0)
  • 2020-12-17 17:19

    You can do by using :

    <script type="text/javascript">
        $(document).ready(function() {
           $(".div/formClass").bind("keypress", function(e) {
              if (e.keyCode == 13) {
                 return false;
              }
           });
        });
    </script>
    
    0 讨论(0)
  • 2020-12-17 17:21
    <%@taglib uri="/struts-tags"  prefix="s" %>
    <html>
    <head>
        <script type="text/javascript">
            function stopsubmit()
            {
                document.ourform.onsubmit="return true";
            }
            function pup()
            {
                return true;
            }
        </script>
    </head>
    <body>
        <form action="sa.jsp" name="ourform" onsubmit="return false">
            <s:submit action="" onclick="stopsubmit()" theme="simple"></s:submit>
        </form>
    </body>
    

    0 讨论(0)
  • 2020-12-17 17:25

    If using MVC3, you can disable submitting via Enter by editing the BeginForm call as follows:

    @using (Html.BeginForm("SomeAction", "SomeController", FormMethod.Post, new { onkeydown = "return event.keyCode!=13" }))
    {
        ...
    }
    
    0 讨论(0)
  • 2020-12-17 17:28

    The problem with the accepted solution is that normal submit buttons no longer work. You must script all the buttons.

    <form onsubmit="return false"></form>
    

    Here's a better solution that doesn't break non javascript submit buttons. This solution simply tells the browser to not do default behavior when a user hits the enter key on form inputs.

    // prevent forms from auto submitting on all inputs
    $(document).on("keydown", "input", function(e) {
      if (e.which==13) e.preventDefault();
    });
    
    0 讨论(0)
  • 2020-12-17 17:34
    <form onsubmit="return false"></form>
    

    This will stop the form proceeding to the next page after either by clicking a submit button or pressing enter.

    0 讨论(0)
提交回复
热议问题