views:

404

answers:

2

Hello, i have a problem with jConfirm, it should work just like the normal javascript confim box but it doesn't, here's an example

<input type="submit" value="click" onClick="return jConfirm('are you sure?')">

If i use the normal confirm it stops the script until a response is given, with jConfirm however, the form is still being submitted, even if no answer is given, any workarounds?

Thanks in Advance

EDIT1: Using Slaks idea i'm trying to tweak the default jAlert plugin, here's what i got.

submit: function(message, title, btnId) {
            $.alerts._show(title, message, null, 'confirm', function(result, btnId) {
                if (result){
                    var form = $('#'+btnId).closest('form');
                    form.trigger('submit');
                }           
                else
                    return false
            });
},

The problem is that i don't know how to pass the 'btnId' variable to the _show callback function, any thoughts?

A: 

Like this:

<input type="submit" value="click" onClick="jConfirm('are you sure?', function(r) { if (r) document.forms[something].submit(); }); return false;">

It would be much better to move that to a separate handler:

$(':submit').click(function(e) {
    e.preventDefault();        //Don't submit until the user hits yes
    var form = $(this).closest('form');
    jConfirm('Are you sure?', function(r) {
        if (r) form.submit();
    });
});
SLaks
I've read that the preventDefault function doesn't work in IE, gives javascript error
Kusanagi2k
@Antonio: jQuery fixes that.
SLaks
A: 

In the end i fixed it like this, sending the button the button (with 'this' while calling the function) i can get the form and then submit it, i can also attach a hidden input with the button pressed so i can execute the proper action with PHP.

submit: function(message, title, btnObj) {
            $.alerts._show(title, message, null, 'confirm', function(result) {
                if (result){
                    var form = $(btnObj).closest('form');
                    var input = '<input type="hidden" name="action" value="'+$(btnObj).attr('value')+'">';
                    $(form).append(input);
                    form.trigger('submit');
                }   
                else
                    return false
            });
        },
Kusanagi2k