Don't refresh page when enter key is pressed

后端 未结 4 1604
失恋的感觉
失恋的感觉 2021-02-09 17:03

I\'m having some problems with the enter key triggering a refresh of the page whenever there is input in a form.

The following code does not refresh the page if enter is

相关标签:
4条回答
  • 2021-02-09 17:22

    you can either add following script at the end of body

    <script>
            $("form").submit(function (e) {
                e.preventDefault();
                alert("call some function here");
            });
    </script>
    

    or you can just put your form tag like this

    <form onsubmit="return false">
    

    either way you will then have to write an onClick="SOMEFUNCTION()" to the input

    also there is an error with an extra /button tag...remove that and instead use

    <input type="button" class="btn btn-default" id="submit" value="Conjugate" />
    

    note the ending slash

    0 讨论(0)
  • 2021-02-09 17:22

    you have syntax error in your html codes:

    <input type="button" class="btn btn-default" id="submit" value="Submit">
                </button>
    

    change it to this:

     <button type="button" class="btn btn-default" id="submit" value="Submit">
                </button>
    

    Also, you need using Javascript/jquery to submit your form to prevent refreshing your entire page

    0 讨论(0)
  • 2021-02-09 17:28

    place : e.preventDefault(); inside the keypress function

    0 讨论(0)
  • 2021-02-09 17:43

    simply change your html:

    <div id="forms" class="container">
            <form action="javascript:void(-1)">
                Enter your verb here in plain (dictionary) form:
                ....
    

    jsfiddle here - works like charm

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