jQuery Deferred and Dialog box

前端 未结 3 1912
function ValidateField(){
var bAllow= true;

    //some checking here

if (bAllow == true && apl.val().trim() == \"\")
{ 
    showDialog(); 
    showDialog()         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 05:55

    why not use the reject method instaed of resolve("false"). You will then be able to pass an object as argument. Let's say you have multiple fieldsets of inputs, each one having a delete button :

     function confirmation(question,obj) {
        var defer = $.Deferred();
        $('
    ') .html(question) .dialog({ autoOpen: true, modal: true, title: 'Confirmation', buttons: { "Oui": function () { defer.resolve(obj);// pass the object to delete to the defer object $(this).dialog("close"); }, "Non": function () { defer.reject();//reject, no need to pass the object $(this).dialog("close"); } }, close: function () { $(this).dialog('destroy').remove() } }); return defer.promise(); } $(document).on("click", ".btn-suppr",function (){// all delete buttons having a class btn-suppr var question = "Are you sure to delete this fieldset ?"; confirmation(question,$(this)).then(function (obj) { obj.parent('fieldset').remove(); // remove the parent fieldset of the delete button if confirmed }); });

提交回复
热议问题