Jquery ui 如何在jQuery UI对话框回调中重新触发表单提交事件
我尝试使用jQuery UI对话框进行某种软验证,如果表单看起来可疑,则警告用户,但允许他们继续提交:Jquery ui 如何在jQuery UI对话框回调中重新触发表单提交事件,jquery-ui,dialog,validation,confirm,Jquery Ui,Dialog,Validation,Confirm,我尝试使用jQuery UI对话框进行某种软验证,如果表单看起来可疑,则警告用户,但允许他们继续提交: // multiple submit buttons... var whichButton; $("#myForm").find("[type=submit]").click(function() { whichButton = this; } var userOkWithIt = false; $("#myForm").submit(function() { if (dataLoo
// multiple submit buttons...
var whichButton;
$("#myForm").find("[type=submit]").click(function()
{
whichButton = this;
}
var userOkWithIt = false;
$("#myForm").submit(function()
{
if (dataLooksFishy() && !userOkWithIt)
{
$("Are you sure you want to do this?").dialog(
{
buttons:
{
"Yes": function()
{
$(this).dialog("close");
// override check and resubmit form
userOkWithIt = true;
// save submit action on form
$("#myForm").append("<input type='hidden' name='" +
$(whichSubmit).attr("name") + "' value='" +
$(whichSubmit).val() + "'>");
$("#myForm").submit(); /****** Problem *********/
},
"No": function() { $(this).dialog("close"); }
}
});
return false; // always prevent form submission here
} // end data looks fishy
return true; // allow form submission
});
我已经用一堆调试警报语句验证了这一点。控制流正是我所期望的。如果我第一次失败dataLooksFishy,我将看到对话框,并且该方法异步返回false
单击“是”会重新触发此方法,而这一次,该方法返回true,但是表单实际上并没有提交
我肯定这里缺少一种更好的方法,但主要目标是能够通过异步对话框模拟同步确认的行为。如果我正确理解了您的问题,下面是解决方案。 我已将操作分离为更易于管理的单独功能: 提交表单通常会检查是否有错误- dataLooksFishy 如果出现错误-应弹出一个对话框 如果用户单击“是”-表单将以force=true提交
var
form = $("#myForm"),
formSubmit = function(force){
if (dataLooksFishy() && force !== true) return showWarning(); // show warning and prevent submit
else return true; // allow submit
},
showWarning = function(){
$("Are you sure you want to do this?").dialog({ buttons: {
"Yes": function(){ $(this).dialog("close"); formSubmit(true); },
"No": function() { $(this).dialog("close"); }
}});
return false;
},
dataLooksFishy = function(){
return true; // true when there are validation errors
};
// plain JS form submitting (also works if you hit enter in a text field in a form)
form[0].onsubmit = formSubmit;
我无法用你的表单测试它,因为你没有在这里发布它。
如果您对此解决方案有问题,请在此处发布更多代码,我将尽力提供帮助。如果我正确理解您的问题,请提供解决方案。 我已将操作分离为更易于管理的单独功能: 提交表单通常会检查是否有错误- dataLooksFishy 如果出现错误-应弹出一个对话框 如果用户单击“是”-表单将以force=true提交
var
form = $("#myForm"),
formSubmit = function(force){
if (dataLooksFishy() && force !== true) return showWarning(); // show warning and prevent submit
else return true; // allow submit
},
showWarning = function(){
$("Are you sure you want to do this?").dialog({ buttons: {
"Yes": function(){ $(this).dialog("close"); formSubmit(true); },
"No": function() { $(this).dialog("close"); }
}});
return false;
},
dataLooksFishy = function(){
return true; // true when there are validation errors
};
// plain JS form submitting (also works if you hit enter in a text field in a form)
form[0].onsubmit = formSubmit;
我无法用你的表单测试它,因为你没有在这里发布它。
如果您对此解决方案有问题,请在此处发布更多代码,我会尽力提供帮助。Whoops..键入错误:whichButton和whichSubmit变量名称应该一致。这部分功能正常…哎呀..输入错误:whichButton和whichSubmit变量名应该一致。那部分功能很好。。。