开发者

jQuery - Ask user before submiting a form

开发者 https://www.devze.com 2023-02-05 11:36 出处:网络
I have a form with 2 submit buttons. <input type=\'submit\' name=\'submit-form\' value=\'Send\' />

I have a form with 2 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开发者_JAVA技巧 it could be done by making the delete button a link, but I really need to do it this way.

Thanks for your time, Mike.


Should look like:

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

by returning false from an event handler, it will trigger a event.preventDefault and event.stopPropagation.

In relation to your comment, I would go this way to have different strings 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/


$('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
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消