我正在使用 jQuery UI对话框 在按钮被点击时显示一个确认对话框。我希望当单击确定时返回
在
以下是代码,所有 CSS/JS 都引用了 jQuery CDN,因此您应该能够复制该代码以查看其行为。
true
,否则返回false
。在
onClick
事件中关联对话框打开调用(如此处所述$dialog.dialog('open');
)并不能达到目的。因此,作为一种解决方法,我遵循了这种方法,与此类似:http://www.perfectline.co.uk/blog/unobtrusive-custom-confirmation-dialogs-with-jquery。这种方法和我的方法之间有两个不同点:
- 示例使用锚标签
- 它没有使用 jQuery UI 对话框
以下是代码,所有 CSS/JS 都引用了 jQuery CDN,因此您应该能够复制该代码以查看其行为。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/themes/blitzer/jquery-ui.css" rel="stylesheet" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Control Panel</title>
</head>
<body>
<form action="http://google.com">
<button class="es-button ui-state-default ui-corner-all" name="changeSem" id="id2">Start Thermonuclear War</span>
</button>
</form>
<div title="Why so serious?" id="id3" style="display:none;">
<p>You are about to start a war.
<p>Click OK to confirm. Click Cancel to cancel this action.</p>
</div>
</body>
<script type="text/javascript">
$('#id2').click(function (event) {
if ($(this).data('propagationStopped')) {
//alert('true');
$(this).data('propagationStopped', false);
return true;
} else {
event.stopImmediatePropagation();
$('#id3').dialog({
//autoOpen: false,
width: 600,
buttons: {
"Ok": function () {
$(this).dialog("close");
$('#id2').data('propagationStopped', true);
$('#id2').triggerHandler(event);
},
"Cancel": function () {
$(this).dialog("close");
return false;
}
}
});
//alert('false');
return false;
}
});
</script>
</html>
澄清:请注意,很容易(请参见bryan.taylor的 解决方案 )进行表单提交。我想在这里做的是通过按钮点击来模拟提交,有点类似于JavaScript的confirm()方法。