jquery 提交内部验证创建了太多递归
因为 jquery 对话框的工作方式,当使用“确认”对话框时,您必须立即返回 false,如果用户选择“确定”,则触发表单提交。
所以,我正在使用这段代码:
function validoForm()
{
//some code here...
if (datosTdcIncompletos==true)
{
var $dialogTDC= $('<div></div>')
.html("TDC information incomplete\n\rDo you want to continue?")
.dialog({
autoOpen: false,
modal:true,
title: 'Confirm',
buttons: {
Ok: function() {$( this ).dialog( "close" ); $('#bookForm').submit();},
Cancel: function() {$( this ).dialog( "close"); return false;}
}
});
$dialogTDC.dialog('open');
return false;
}
$('#bookForm').submit();
}
并且
<script type="text/javascript">
$(document).ready(function() {
$('#submitBtn').click(function (){ $('#bookForm').submit();});
var options1 = {
target: '#bookForm',
url: 'http://localhost/include/processForm.php',
type:'post',
beforeSubmit:validoForm,
success: showResponse
};
$('#bookForm').ajaxForm(options1);
});
</script>
问题的出现是由于 Ok 按钮函数 (Ok: function() {$( this ).dialog( "close" ); $('#bookForm').submit ();},
) 因为这是再次提交表单,我收到“太多递归”错误。
这该怎么办?
because the way jquery dialogs woks, when using a "confirm" dialog, you have to return false immediately, and if the user selects "Ok", then trigger the form submit.
so, I'm using this code:
function validoForm()
{
//some code here...
if (datosTdcIncompletos==true)
{
var $dialogTDC= $('<div></div>')
.html("TDC information incomplete\n\rDo you want to continue?")
.dialog({
autoOpen: false,
modal:true,
title: 'Confirm',
buttons: {
Ok: function() {$( this ).dialog( "close" ); $('#bookForm').submit();},
Cancel: function() {$( this ).dialog( "close"); return false;}
}
});
$dialogTDC.dialog('open');
return false;
}
$('#bookForm').submit();
}
and
<script type="text/javascript">
$(document).ready(function() {
$('#submitBtn').click(function (){ $('#bookForm').submit();});
var options1 = {
target: '#bookForm',
url: 'http://localhost/include/processForm.php',
type:'post',
beforeSubmit:validoForm,
success: showResponse
};
$('#bookForm').ajaxForm(options1);
});
</script>
The problem arises because of the Ok button function (Ok: function() {$( this ).dialog( "close" ); $('#bookForm').submit();},
) because this is submitting the form again and I get a "too much recursion" error.
how should this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 SubmitBtn 点击处理程序应该只运行验证方法(并返回 false 以阻止默认机制)。
那么你的验证方法应该只在用户确认确定时调用提交。
您还需要删除最后一个 $('#bookForm').submit();在您的验证方法中,它会一直被调用。
Your submitBtn click handler should just run the validation method (and return false to prevent the default mechanism).
Then your validation method should only call submit when the user confirms OK.
You also need to remove the last $('#bookForm').submit(); in your validation method, that gets called all the time.
您需要整个验证和对话作为一个实体来工作。因此,当单击提交按钮时,JavaScript 验证就会启动,并且对话框会一次性显示。然后对话作为验证过程的最后一步。
因此,在验证方法结束时,您应该返回从对话返回的值或返回 true(假设当表单不完整时对话会显示)。
这也意味着删除提交按钮上的单击事件,否则您将像您所看到的那样陷入循环。
You need the entire validation and dialogue to work as a single entity. So when the submit button is clicked, the javascript validation kicks in and the dialogue shows in one go. Then the dialogue acts as the final step in the validation process.
So at the end of your validate method you should return the value returned from the dialogue or return true (assuming the dialogue shows when the form is incomplete).
That also means removing the click event on the submit button as otherwise you'll just go around in a loop as you have seen.