简体   繁体   中英

No jquery events working properly inside form tag

    <script type="text/javascript">

        $("document").ready(function () {

            $("#done").click(function () {
                $("#nm").hide();

            });
        });
    </script>

But when I use form tag it doesn't work. When I run the code, the validation stayed for few seconds and then gone.. But in the case when I remove the <form> , it runs well. Why does this happen?

    <form id="frm1" method="post"  >
        Name:    <input type="text" id="nm"/><br>
        E-Mail:  <input type="email" id="eml"/><br>
        Password:<input type="password" id="pass"/><br>

        <input type="submit" id="done"  value="SUBMIT"/>


    </form>       

you need to pass an event and use preventDefault() to prevent the form from submitting on click:

$("#done").click(function (event) {
   event.preventDefault();
   $("#nm").hide();
 });

You could use the event as in the example above or avoid using it and return false instead:

        $("#done").click(function () {
            $("#nm").hide();
            return false;
        });

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