问题描述
我阅读了很多关于此的问题,但是每个解决方案都使用相同的解决方法,在jquery对话框中提交表单,如下所示:
I read lots of questions about this, but every solution uses the same workaround, submiting the form inside the jquery dialog, something like this:
$("#dialog").dialog({
buttons : {
"Confirm" : function() {
window.location.href = targetUrl;
},
"Cancel" : function() {
$(this).dialog("close");
}
}
是不是有更简单的方法,更像是javascript确认?
Isn't there an easier way, more like javascript confirm?
<input type="submit" onclick="return confirm('are you sure?');" />
为什么返回true,返回false不起作用?
Why something like return true, return false doesn't work?
推荐答案
这是您可以做的,您将输入类型从提交更改为按钮,然后,您可以像这样使用您的脚本:
Here's what you can do, you change your input type from 'submit' to 'button', then, you can have your script like this:
$(document).ready(function(){
$("#dialog").dialog({
autoOpen: false,
buttons : {
"Confirm" : function() {
$('#form1').submit();
},
"Cancel" : function() {
$(this).dialog("close");
}
}
});
$('#submitButton').click(function(){
$("#dialog").dialog('open');
});
});
这样,只有在使用确认对话框时才会提交表单。
This way your form will only be submitted when the used confirms the dialog.
在你的情况下返回false或true无关紧要的原因是对话框刚刚显示但是submit事件中的代码一直在执行,除非你在显示对话框后返回false 。
The reason it doesn't matter if you return false or true in your case is that the dialog is just shown but code from the submit event keeps on executing unless you return false just after showing the dialog.
这篇关于如何使用jquery UI对话框作为javascript确认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!