2009-12-03 12 views
7

मैं इसे कैसे प्राप्त कर सकता हूं?jQuery/जावास्क्रिप्ट के साथ एक पुष्टिकरण संदेश बनाने का सबसे आसान तरीका?

  1. कोई उपयोगकर्ता हटाएं लिंक ("पुष्टि" की श्रेणी के साथ) पर क्लिक करता है।
  2. पुष्टिकरण संदेश पूछता है "क्या आप निश्चित हैं?" "हां" और "रद्द करें" विकल्पों के साथ।

यदि हां चुना गया है, तो लिंक क्लिक के रूप में जारी है, लेकिन यदि रद्द किया गया है, तो कार्रवाई रद्द कर दी गई है।

अद्यतन: अंतिम काम कर confirm() धन्यवाद के साथ this guy करने के लिए कोड:

$('.confirm').click(function() { 
    return confirm("Are you sure you want to delete this?"); 
}); 
+0

आप (document) .ready एक $ में रख किया था() {}? – helloandre

+0

हां, यह मेरे दस्तावेज़ में तैयार है। – Andrew

उत्तर

17

जावास्क्रिप्ट अंतर्निहित पुष्टिकरण संवाद प्रदान करता है।

if (confirm("Are you sure?")) 
{ 
    // continue with delete 
} 
+0

आपको ठीक और विकल्प रद्द कर देगा। –

+0

इसे आसानी से मार नहीं सकता है। – nickf

+0

सबसे सरल जवाब आमतौर पर सही है :) – AntonioCS

0

मैंने सफलतापूर्वक Jquery में पुष्टिकरण बॉक्स लागू किया है। सुनिश्चित करें कि आपके पास यह कोशिश करने से पहले आपके कोड में Jquery लाइब्रेरी और सीएसएस शामिल है (jquery-ui-1.8.16.custom.css, jquery-1.6.2.js, jquery-ui-1.8.16.custom.min। जे एस)। जावास्क्रिप्ट कन्फर्म बॉक्स और इस बॉक्स के बीच मुख्य अंतर जो हम DIV का उपयोग करते हैं - क्या यह है - जावास्क्रिप्ट कन्फर्म उपयोगकर्ता इनपुट के लिए इंतजार कर रहा है, उपयोगकर्ता प्रविष्टियों के बाद हाँ/नहीं अगला लाइन निष्पादित होगी, यहां आपको ऐसा करना होगा, या कोई ब्लॉक - ** showConfirm (के बाद कोड की अगली लाइन) निष्पादित करेंगे तुरंत * इसलिए सावधान रहना

/** add this div to your html 

*/

var $confirm; 
var callBack; 
var iconStyle = '<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 50px 0;"></span>'; 
var messageStyleStart = '<span align="center" style="font-family:arial, verdana, sans-serif;font-size:9.3pt;">'; 
var messageStyleEnd = '</span>'; 


$(document).ready(function(){ 
    $('#confirmDialog').dialog({ 
      autoOpen: false, 
      modal: true 
    }); 


    //jquery confirm box -- the general alert box 
    $confirm = $('<div style="vertical-align:middle;"></div>') 
    .html('This Message will be replaced!') 
    .dialog({ 
     autoOpen: false, 
     modal: true, 
     position: 'top', 
     height:300, 
     width: 460, 
     modal: true, 
     buttons: { 
      Ok : function() { 
       $(this).dialog("close"); 
       if(null != callBack) 
        callBack.success(); 
      }, 
      Cancel: function() { 
       $(this).dialog("close"); 
       if(null != callBack) 
        callBack.fail(); 
      } 
     } 
    }); 

}); 

    function showConfirm(message,callBackMe,title){ 
    callBack = null; 
    $confirm.html(""); // work around 
    $confirm.html(iconStyle + messageStyleStart +message + messageStyleEnd); 
    if(title =='undefined'|| null ==title) 
     $confirm.dialog("option", "title", "Please confirm"); 
    else 
     $confirm.dialog("option", "title", title); 
    var val = $confirm.dialog('open'); 
    callBack = callBackMe; 
    // prevent the default action 
    return true; 
} 

    // Now for calling the function 
// create a Javascript/jSOn callback object 

var callMeBack = { 
        success: function() 
          { // call your yes function here         
           clickedYes(); 
           return; 
          }, 
        fail: function(){ 
           // call your 'no' function here 
           clickedNo(); 
           return ; 
          } 
       }; 


    showConfirm("Do you want to Exit ?<br/>"+ 
        ,callMeBack1,"Please Answer"); 
1
मेरे अनुभव में

एक पुष्टि करने के लिए यह सबसे अच्छा और आसान तरीका है!

<a href="#" onclick="return myconfirm()">Confirm</a> 
<script> 
function myconfirm() 
{ 
    if(confirm('Are You Sure ...')) 
     return true; 
    return false; 
} 
</script> 
+1

पुष्टिकरण फ़ंक्शन के वापसी मूल्य का उपयोग करने का एक आसान तरीका होगा: 'onclick = "वापसी की पुष्टि करें (' क्या आप निश्चित हैं? ')" ' – Andrew

संबंधित मुद्दे