jQuery - combining "confirm" and "someOtherAction"
I have the following code that simply uses a jquery ui modal dialog to "confirm" any click on a link. If they click OK, then it sends them to the original link. It works fine, so don't bother reading it in too much detail =)
$('.confirm').live('click', function(e) {
e.preventDefault();
var theHref = $(this).attr('href');
var confirmText = 'Are you sure?';
if ($(this).attr('confirm')) {
confirmText = $(this).attr('confirm');
}
confirmDialog.html(confirmText);
confirmDialog.dialog({
buttons: {
'OK': function() {
window.location = theHref;
$(this).dialog('close');
},
Cancel: function() {
$(this).dialog('close');
return false;
}
}
});
confirmDialog.dialog('open');
return false;
});
Then, I have a bunch of other arbitrary functionality. Like, say this:
$('a.reset').live('click', function(e) {
e.preventDefault();
$(this).parents('form')[0].reset();
});
That's just a link that resets a form (like an input type = reset). This functionality also works fine. Please note this is just an arbitrary example. I've got plenty of different snippets of JS functionality that I att开发者_如何学编程ach via classes.
The problem I am encountering is stacking these two. In this example, some reset links I might want confirmations, some I might not. Like so:
<a href="" class="reset confirm">Reset on confirmation</a>
<a href="" class="reset">Reset without confirmation</a>
The problem of course, is that the first example doesn't care about the confirm - it just goes ahead and resets. Is there some way I can continue to keep these bits of code separate and modular (like in the HTML example), while at the same time making one dependent on the output of the other?
I hope it is clear what I'm trying to achieve.
Thanks in advance for any assistance. First time posting on here so be nice =)
I don't know if this will work, but try creating the .live handlers in reverse order. I think they might get triggered from last attached to first attached.
well one very simple approach ill guess from what i understood you want to achieve would be to check whether the element has the confirmation class attached to it before you prevent any default behaviour and what not
I think you can probably sort this out by simply attaching the code to the confirm class before the reset class. Just put it higher up in your document.ready or wherever you specify the events.
精彩评论