JQuery - ask user before submitting form

I have a form with two submit buttons.

<input type='submit' name='submit-form' value='Send' /> <input type='submit' class='delete' name='delete' value='Delete' /> 

And I want to ask the user if he really wants to delete the item. I know that this can be done by making the delete button a link, but I really need to do it this way.

Thanks for your time, Mike.

+4
source share
2 answers

It should look like:

 $('input.delete').bind('click', function() { if(!confirm('are you sure') ) return false; }); 

returning false from the event handler, it calls the event.preventDefault and event.stopPropagation .

As for your comment, I would go in such a way as to have different lines for different inputs:

 $('input').bind('click', function(e) { var msg; switch(e.className) { case 'foobar': msg = 'Foo foo foo!?'; break; case 'yay': msg = 'yay yay yay!?'; break; // etc default: msg = 'are you sure?'; } if(!confirm(msg) ) return false; }); 

Demo: http://www.jsfiddle.net/ks9Ak/

+5
source
 $('input.delete').click(function(e){ e.preventDefault(); var action = confirm('do you want to delete the item?'); if(action){ //delete the item the way you want } }); 
+1
source

Source: https://habr.com/ru/post/1335750/


All Articles