jQuery的submit()和AJAX事件;(jQuery submit() and AJAX event; How can I trigger a regular form submit?)

编程入门 行业动态 更新时间:2024-10-28 01:14:52
jQuery的submit()和AJAX事件;(jQuery submit() and AJAX event; How can I trigger a regular form submit?)

我正在开发一个项目,在点击提交按钮时,我会捕获事件并通过外部服务运行表单以检查某些内容是否有效。

如果出现问题,我会弹出一个对话框告诉用户存在问题,并让他们选择重写继续。

问题是如何触发表单提交。 如果我只使用.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.

更多推荐

本文发布于:2023-07-19 17:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1183973.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   AJAX   submit   jQuery   form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!