如何在jquery-alerts.js中的jConfirm条件中停止执行

时间:2016-06-01 15:33:12

标签: jquery jconfirm

请检查以下代码:

在确认确认对话框之前,我不想执行alert('End');

<link href="http://labs.abeautifulsite.net/archived/jquery-alerts/demo/jquery.alerts.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://labs.abeautifulsite.net/archived/jquery-alerts/demo/jquery.alerts.js"></script>
<script type="text/javascript">

			$(document).ready( function() {
            $("#confirm_button").click( function() {
					alert('Start');
                    jConfirm('Can you confirm this?', 'Confirmation Dialog', function(r) {
	                if(r){alert('Ok');}else{alert('Cancel');}
             });
					 alert('End');
			 });
});

		</script>
<fieldset>
			<legend>Confirm</legend>
 
			<p>
				<input id="confirm_button" type="button" value="Show Confirm">
			</p>
		</fieldset>

1 个答案:

答案 0 :(得分:0)

当你调用异步函数时(例如jConfirm,另一个popuplar示例是ajax()),你不能停止执行代码。

当您使用异步功能时,您还必须使用回调函数。

关闭对话框后需要执行的代码需要进入function(r) { ... } - 其他所有内容都将并行执行。

jConfirm('Can you confirm this?', 'Confirmation Dialog', function(r) {
    if(r){alert('Ok');}else{alert('Cancel');}
    // Put your code here and it will be executed when the dialog is closed.
});

&#13;
&#13;
<link href="http://labs.abeautifulsite.net/archived/jquery-alerts/demo/jquery.alerts.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://labs.abeautifulsite.net/archived/jquery-alerts/demo/jquery.alerts.js"></script>
<script type="text/javascript">

			$(document).ready( function() {
            $("#confirm_button").click( function() {
					alert('Start');
                    jConfirm('Can you confirm this?', 'Confirmation Dialog', function(r) {
	                if(r){alert('Ok');}else{alert('Cancel');}
                    alert('End');
             });
					 
			 });
});

		</script>
<fieldset>
			<legend>Confirm</legend>
 
			<p>
				<input id="confirm_button" type="button" value="Show Confirm">
			</p>
		</fieldset>
&#13;
&#13;
&#13;