我使用jQuery UI dialog在单击按钮时显示确认对话框。我想返回true
,点击确定后,否则返回false
。使用JQuery UI对话框从确认对话框中返回值
onClick
(如给出的here,$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>
澄清:请注意,这是很简单的(见solution by bryan.taylor)做一个表单提交。我想在这里做的是通过点击按钮模拟提交。更像是JavaScript的confirm()方法。
理想的情况下,这个例子应该带你到谷歌确定关闭并做取消什么。我会尽量避免显式$(formName).submit()的解决方案。 – Nishant 2010-08-24 21:02:56
可能重复[jquery ui对话框需要返回值,当用户按下按钮,但不工作](http://stackoverflow.com/questions/6049687/jquery-ui-dialog-box-need-to-return-价值当用户按下按钮但不是沃尔) – Liam 2013-08-20 13:28:22