jQuery 模式对话框未提交我的表单
我正在使用 jQuery 模态对话框询问用户是否希望提交表单。
但是,在用户单击对话框的“提交”按钮后,表单并未提交。
如果我再次单击表单提交按钮,它就会提交。
我猜这是一个范围问题,我看过其他一些关于它的帖子,但到目前为止已经花了很多时间但没有运气。关于如何解决这个问题有什么想法吗?
JavaScript
$(document).ready(function(){
var submitForm = $('#myform');
submit = false;
$("#confirm").dialog({
resizable: false,
height: 140,
modal: true,
autoOpen: false,
buttons: {
'Submit': function() {
$(this).dialog('close');
submit = true;
submitForm.submit();
},
'Cancel': function() {
$(this).dialog('close');
}
}
});
$("#confirm").parent().appendTo($("#myform"));
submitForm.submit(function() {
if (submit) {
return true;
} else {
$("#confirm").dialog('open');
return false;
}
});
});
HTML
<form id="myform" action="#" method="post">
<input type="text" name="check_me" />
<input type="submit" name="submit" value="Go!" />
</form>
<div id="confirm" style="display:none;">Please confirm that you want to submit</div>
I am using a jQuery Modal Dialog box to ask the user if they wish to submit the form or not.
However after the user clicks Dialog's Submit button, the form is not submitting.
If I then click the forms submit button again, it submits.
I am guessing this is a scope issue, I have seen some other posts about it but as yet have spent many hours with no luck. Any ideas on how to solve this?
Javascript
$(document).ready(function(){
var submitForm = $('#myform');
submit = false;
$("#confirm").dialog({
resizable: false,
height: 140,
modal: true,
autoOpen: false,
buttons: {
'Submit': function() {
$(this).dialog('close');
submit = true;
submitForm.submit();
},
'Cancel': function() {
$(this).dialog('close');
}
}
});
$("#confirm").parent().appendTo($("#myform"));
submitForm.submit(function() {
if (submit) {
return true;
} else {
$("#confirm").dialog('open');
return false;
}
});
});
HTML
<form id="myform" action="#" method="post">
<input type="text" name="check_me" />
<input type="submit" name="submit" value="Go!" />
</form>
<div id="confirm" style="display:none;">Please confirm that you want to submit</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案很简单...您的按钮名称。你为什么问?我会告诉你!
拿这个 jsfiddle 并注意我只做了一些更改。 javascript 没有改变,只是 HTML
HTML
我做了两处更改:
我必须做#2 因为调用 $('#myform').submit 引用的是按钮,而不是提交方法,并且 jQuery 遇到了各种困惑。通过重命名按钮,$('#myform').submit 仍然是预期的 jQuery 提交函数,每个人都很高兴。
我通过几次迭代偶然发现了这一点,我将其保留在下面以供后代使用。
祝你好运!
=======下面的原始帖子=======
如果您只想“解决这个问题”,您可以重构如下:
HTML
JavaScript
奇怪的是,下面的代码有效还有:
HTML
JavaScript
我在这里唯一改变的是删除了提交按钮和 100% 通过 jQuery 提交。
The solution is simply... your button name. Why you ask? I shall show you!
Take this jsfiddle and notice I only made a few changes. The javascript has not changed, just the HTML
HTML
I've made two changes:
I had to do #2 because calling $('#myform').submit was referencing the button, not the submit method, and jQuery got all kinds of confused. By renaming your button, the $('#myform').submit remains the expected jQuery submit function and everybody is happy.
I stumbled upon this through going through several iterations, which I've kept below for posterity.
Good luck!
=======ORIGINAL POST BELOW========
If all you want to do is "solve this", you can refactor as follows:
HTML
JavaScript
Oddly, the below works as well:
HTML
JavaScript
The only thing I changed here was removed the submit button and 100% submit via jQuery.
将提交按钮的名称属性从“submit”更改为“btnSubmit”。
将表单操作属性中的“#”替换为空白或任何其他有效的 URL。
Change the name attribute of your submit button from 'submit' to 'btnSubmit'
Replace the '#' in the form action attribute to blank or any other valid url.
您可以尝试在提交事件表单上添加不同类型的事件。
请参阅此 编辑 ,现在您可以使用模态对话框返回更改创建一个函数是真是假。
You can try on submit event form have different type of events.
see this EDIT , now you can change create one function with Modal Dialog return true or false.