2012-12-07 12 views
11
function ValidateField(){ 
var bAllow= true; 

    //some checking here 

if (bAllow == true && apl.val().trim() == "") 
{ 
    showDialog(); 
    showDialog().done(function() { 
     return true; // wanna return true, but not success 
    }).fail(function() { 
     return false; //wanna return false, but not success 
    }); 
    return false; //stop it to execute to next line 
} 
return bAllow; //success return } 

function showDialog(){ 
var def = $.Deferred(); 
var modPop = '<div id="diaCom" title="Information?"><p>something something</p></div>'; 
$("#diaCom").remove(); 
$(modPop).appendTo('body'); 
$("#diaCom").dialog({ 
    resizable: false, 
    draggable: false, 
    height:150, 
    width:300, 
    modal: true, 
    buttons: { 
     "Ok": function() { 
      def.resolve(); 
      $(this).dialog("close"); 

     }, 
     "Cancel": function() { 
      def.reject(); 
      $(this).dialog("close"); 

     } 
    } 
}); 

return def.promise(); 
} 
//on click 
if (validateField() == true){ 
     //do something 
}else{ 
     //do something 
    } 

हाय सबको, मूल्य वापस करने का कोई मौका? मैं showDialog() के माध्यम से सत्य और गलत लौटना चाहता हूं।() किया गया है (वैध)() फ़ंक्शन के लिए असफल रहा है, लेकिन यह मेरे काम के रूप में काम नहीं कर रहा है, मैं इसे आवंटित नहीं कर सकता क्योंकि मेरे पास पहले से ही झूठी वापसी है अपनी अगली पंक्ति, किसी भी विचार को निष्पादित करने के लिए संवाद? या ऐसा करना सही है?jQuery डिफरर्ड और डायलॉग बॉक्स

+0

आप आस्थगित वस्तु के पूरा होने के लिए प्रतीक्षा करने जावास्क्रिप्ट पैदा करने के लिए सक्षम नहीं होगा, तो कोई रास्ता नहीं करने के लिए नहीं है 'वापसी बाद में मुझे यकीन नहीं है कि इसे कैसे "ठीक करें", लेकिन मुझे लगता है कि आपका कोड/तर्क – Ian

+0

पुनर्गठन करना है, नहीं, आप एक असीमित कार्य से सिंक्रनाइज़ नहीं कर सकते हैं। इसके बजाय, स्थगित पास! – Bergi

+0

तो इसका मतलब है कि मैं किसी अन्य फ़ंक्शन पर जाता हूं, n उसी फ़ंक्शन के बजाय सत्यापन जांच करने के लिए अन्य फ़ंक्शन का उपयोग करता हूं? मुझे ऐसा करने का थोड़ा सा सही तरीका दिखा सकता है? एक ग्लोबल वेरिएबल बनाएं, इसे डिफ्रेंड के आधार पर सही/फेज पर सेट करें, फिर किसी अन्य फ़ंक्शन में, ग्लोबल वैरिएबल का उपयोग करने के लिए जांच करें, उसके बाद फ़ंक्शन को सही/गलत लौटने के लिए उपयोग करें?मैं अभी भी डिफर्ड करने के लिए नया हूं, बस Google n पहेली के माध्यम से इसे खोजें, इस – Se0ng11

उत्तर

27

अच्छा, यह काम कर सकता है।

आपका संवाद समारोह ... showDialog()

function confirmation(question) { 
    var defer = $.Deferred(); 
    $('<div></div>') 
     .html(question) 
     .dialog({ 
      autoOpen: true, 
      modal: true, 
      title: 'Confirmation', 
      buttons: { 
       "Yes": function() { 
        defer.resolve("true");//this text 'true' can be anything. But for this usage, it should be true or false. 
        $(this).dialog("close"); 
       }, 
       "No": function() { 
        defer.resolve("false");//this text 'false' can be anything. But for this usage, it should be true or false. 
        $(this).dialog("close"); 
       } 
      }, 
      close: function() { 
       $(this).remove(); 
      } 
     }); 
    return defer.promise(); 
} 

और उसके बाद कोड जहां समारोह का उपयोग करें ...

function onclick(){ 
    var question = "Do you want to start a war?"; 
    confirmation(question).then(function (answer) { 
     var ansbool = Boolean.parse(answer.toString()); 
     if(ansbool){ 
      alert("this is obviously " + ansbool);//TRUE 
     } else { 
      alert("and then there is " + ansbool);//FALSE 
     } 
    }); 
} 

यह ज्यादातर लोगों के लिए गलत लग सकता है। लेकिन हमेशा कुछ स्थितियां होती हैं जहां आप JQuery संवाद से वापस बिना वापस नहीं जा सकते हैं।

यह मूल रूप से पुष्टिकरण() फ़ंक्शन की नकल करेगा। लेकिन बदसूरत कोड और एक अच्छी पुष्टि बॉक्स के साथ देखो :)

मुझे आशा है कि इससे कुछ लोगों की मदद मिल जाएगी।


संपादित: 3 समाधान


बूटस्ट्रैप मैं अब NakuPanda's बूटस्ट्रैप पुस्तकालय का उपयोग कर रहा, यह वास्तव में अच्छी तरह से काम करता है। मूल रूप से JQueryUI के साथ ही बूटस्ट्रैप UI में समान है।

function bsConfirm(question) { 
    var defer = $.Deferred(); 
    BootstrapDialog.show({ 
     type: BootstrapDialog.TYPE_PRIMARY, 
     title: 'Confirmation', 
     message: question, 
     closeByBackdrop: false, 
     closeByKeyboard: false, 
     draggable: true, 
     buttons: [{ 
      label: 'Yes', 
      action: function (dialog) { 
       defer.resolve(true); 
       dialog.close(); 
      } 
     }, { 
      label: 'No', 
      action: function (dialog) { 
       defer.resolve(false); 
       dialog.close(); 
      } 
     }], 
     close: function (dialog) { 
      dialog.remove(); 
     } 
    }); 
    return defer.promise(); 
} 
function bsAlert(error, message) { 
    BootstrapDialog.show({ 
     type: error ? BootstrapDialog.TYPE_DANGER : BootstrapDialog.TYPE_SUCCESS, 
     title: error ? "Error" : "Success", 
     message: message, 
     closeByBackdrop: false, 
     closeByKeyboard: false, 
     draggable: true, 
     buttons: [{ 
      label: 'OK', 
      action: function (d) { 
       d.close(); 
      } 
     }] 
    }); 
} 

और उसका उपयोग करना (सुंदर ठीक उसी तरह)

bsConfirm("Are you sure Bootstrap is what you wanted?").then(function (a) { 
    if (a) { 
     bsAlert("Well done! You have made the right choice"); 
    } else { 
     bsAlert("I don't like you!"); 
    } 
}); 
+7

मैंने कई बार एक समान दृष्टिकोण का उपयोग किया है। एनबी: आपका कोड डीओएम में कॉन्फ़िगर किया गया '

' छोड़ देगा - आपको '$ (यह) .dialog ('नष्ट') की आवश्यकता है। डायलॉग के 'क्लोज़' हैंडलर में हटाएं()'। – Alnitak

+0

@Pierre: क्या मुझे डिफर्ड के लिए कोई अतिरिक्त लाइब्रेरी जोड़नी है और काम करने का वादा करना है? – ChandniShah

+0

@ कंधनीशह केवल Jquery 1.5+ यह एक अंतर्निहित कार्य है। अधिक जानकारी के लिए 'https: // api.jquery.com/श्रेणी/स्थगित-ऑब्जेक्ट /' देखें – Pierre

4

मैं this JSFIDDLE बनाया है और बूलियन पार्स बदल क्योंकि उस उड़ा दिया गया था। धन्यवाद, पियरे! इसने मुझे बहुत समय बचा लिया है।

जावास्क्रिप्ट:

function confirmation(question) { 
var defer = $.Deferred(); 
$('<div></div>') 
    .html(question) 
    .dialog({ 
     autoOpen: true, 
     modal: true, 
     title: 'Confirmation', 
     buttons: { 
      "Yes": function() { 
       defer.resolve("true");//this text 'true' can be anything. But for this usage, it should be true or false. 
       $(this).dialog("close"); 
      }, 
      "No": function() { 
       defer.resolve("false");//this text 'false' can be anything. But for this usage, it should be true or false. 
       $(this).dialog("close"); 
      } 
     }, 
     close: function() { 
      //$(this).remove(); 
      $(this).dialog('destroy').remove() 
     } 
    }); 
return defer.promise(); 
}; 

function onclick(){ 
var question = "Do you want to start a war?"; 
confirmation(question).then(function (answer) { 
    console.log(answer); 
    var ansbool = (String(answer) == "true"); 
    if(ansbool){ 
     alert("this is obviously " + ansbool);//TRUE 
    } else { 
     alert("and then there is " + ansbool);//FALSE 
    } 
}); 
} 

$("#item").on('click', onclick); 

HTML:

<button id="item">Hello, click me.</button> 
1

क्यों उपयोग नहीं अस्वीकारेंनीचे विधि संकल्प की instaed ("झूठे")। फिर आप ऑब्जेक्ट को तर्क के रूप में पास करने में सक्षम होंगे। मान लीजिए कि आप आदानों की कई fieldsets करते हैं, हर एक एक हटाएँ बटन होने:

function confirmation(question,obj) { 
    var defer = $.Deferred(); 
    $('<div></div>') 
     .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 

          }); 
         }); 
संबंधित मुद्दे