简体   繁体   中英

Don't work alert code jQuery

I want to get an alert after click on submit button. I tried with the following code, but it doesn't work. How can it be done?

DEMO: http://jsfiddle.net/7atv7zgp/

  $('body').delegate('.submit', 'submit', function(e) { e.preventDefault(); alert('ok'); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="#" method="post"> <input type="text" name="addres" id="keyword-txt5"> <input type="submit" value="submit" class="submit"> </form> 

   $('body').on('submit','form', function(e) {
            e.preventDefault();
            alert('ok');
        })

http://jsfiddle.net/7atv7zgp/2/

Form element have submit event , not button it self. (also, i've used on() method, rather than delegate(), but you can use delegate() if you need older version of jquery).

Simply bind the click event with the button via a class 'submit' and it will solve your purpose

$('.submit').click(function(e) {
      e.preventDefault();
      alert('ok');
});

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