我正在开发一个项目,在点击提交按钮时,我会捕获事件并通过外部服务运行表单以检查某些内容是否有效。
如果出现问题,我会弹出一个对话框告诉用户存在问题,并让他们选择重写继续。
问题是如何触发表单提交。 如果我只使用.submit(),它会将表单再次发送到对话框中。
$('#editAddr').submit(function(){ var checkString = addrString(); $.getJSON('/validate.php' + checkString, function(data){ if(data == 0){ var confw = ''; confw += '<div><p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>'; confw += 'Failed validation.</p></div>'; var $dialog = $(confw).dialog({ title: "Validation Problem", modal: true, resizable: false, width: 500, buttons: { "Edit": function(){ $(this).dialog('close'); }, "Override": function(){ $(this).dialog('close'); $('#editAddr').submit(); } } }); }else{ $('#editAddr').submit(); } }); return false; });如何通过点击对话框按钮来正确提交此表单?
编辑:确保你的表单不包含id =“submit”的按钮,否则你会打破jQuery .submit()
I'm working on a project where when the submit button is clicked, I catch the event and run the form through an external service to check that something is valid.
If there is a problem, I pop a dialog telling the user that there was a problem, and give them the option of overriding to continue on.
The problem is how to trigger the form submit. If I just use .submit(), it send the form into the dialog catch again.
$('#editAddr').submit(function(){ var checkString = addrString(); $.getJSON('/validate.php' + checkString, function(data){ if(data == 0){ var confw = ''; confw += '<div><p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>'; confw += 'Failed validation.</p></div>'; var $dialog = $(confw).dialog({ title: "Validation Problem", modal: true, resizable: false, width: 500, buttons: { "Edit": function(){ $(this).dialog('close'); }, "Override": function(){ $(this).dialog('close'); $('#editAddr').submit(); } } }); }else{ $('#editAddr').submit(); } }); return false; });How can I submit this form properly through the dialog button click?
EDIT: Make sure your form does not include a button with the id="submit", or you will break the jQuery .submit()
最满意答案
而不是绑定到表单的提交事件,而是绑定到提交按钮的点击事件。 这样,您可以捕获提交事件的点击事件,并在需要时返回false,或者继续执行表单上的submit()事件。
$('input[type="submit"]').click(function(e){ e.preventDefault(); if() // invalid data { } else // good to go { $('form').submit(); } })如果有人禁用JavaScript,此解决方案也会很好地降级。 我读过的其他一些解决方案不会允许这样做。
Instead of binding to the submit event of your form, bind to the click event of the submit button instead. This way you can catch the click event of the submit event and return false when you need to, or proceed with the submit() event on the form.
$('input[type="submit"]').click(function(e){ e.preventDefault(); if() // invalid data { } else // good to go { $('form').submit(); } })This solution will also degrade nicely if someone disabled JavaScript. Some of the other solutions I've read won't allow this.
更多推荐
发布评论