javascript - combining two functions to check if true then continue -
i have 2 triggers, 1 returns boolean
confirm
, other sends ajax request
.
i noticed using functions are, if click cancel ajax still follows through. issue confirm trigger
broad , used more 1 trigger used different things. how go checking if confirm returns false?
heres example:
this code check see if confirm link clicked
$('div.answers').on('click', '.confirm', function() { return confirm('are sure?'); });
this code ajaxy things has confirm
class:
$('div.answers').on('click', '.trg-delete', function() { var = $(this); var itemid = that.data("id"); $.ajax({ type: "post", url: "./delete-answer.php", data: { itemid: itemid } }) .done(function(data) { if(data == 1) { .css("color", "#27ae60") .html('<i class="fa fa-fw fa-check"></i>'); } else { .css("color", "#c0392b") .html('<i class="fa fa-fw fa-close"></i>'); }; window.settimeout(function() { .css("color", "") .html('<i class="fa fa-fw fa-close"></i>'); }, 5000); }) .fail(function(data) { .css("color", "#c0392b") .html('<i class="fa fa-fw fa-close"></i>'); window.settimeout(function() { .css("color", "") .html('<i class="fa fa-fw fa-close"></i>'); }, 5000); }); });
as can see, these 2 separate have triggers such trg-open
, trg-closed
, trg-choice
totally different things, have confirm
class.
what easiest way edit, in example, trg-delete
on click check see if confirm
on click returns true
or false
?
note: did see this different using on('click')
s has functions.
why build 2 separate events?
why not on .trg-delete
(note second line):
$('div.answers').on('click', '.trg-delete', function() { if( confirm( "are sure?" ) ) { var = $(this); var itemid = that.data("id"); $.ajax({ type: "post", url: "./delete-answer.php", data: { itemid: itemid } }) .done(function(data) { if(data == 1) { .css("color", "#27ae60") .html('<i class="fa fa-fw fa-check"></i>'); } else { .css("color", "#c0392b") .html('<i class="fa fa-fw fa-close"></i>'); }; window.settimeout(function() { .css("color", "") .html('<i class="fa fa-fw fa-close"></i>'); }, 5000); }) .fail(function(data) { .css("color", "#c0392b") .html('<i class="fa fa-fw fa-close"></i>'); window.settimeout(function() { .css("color", "") .html('<i class="fa fa-fw fa-close"></i>'); }, 5000); }); } });
Comments
Post a Comment