请检查以下代码:
在确认确认对话框之前,我不想执行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>
答案 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.
});
<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;