在提交表单之前执行操作? [英] Perform action before form submit?

查看:74
本文介绍了在提交表单之前执行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有ID的HTML表单,并且已将此ID绑定到Submit函数.我想要的是,当用户单击提交"时,会弹出一个弹出窗口,就像一个模式框,感谢他们.而且只有当他们单击屏幕或模式框关闭后,我才需要表单提交并刷新页面.

I have this HTML form with an id and I have binded this id to the submit function. What I want is when a user clicks submit, a popup comes up like a modal box thanking them. And only after they click off the screen or when the modal box closes is when I want the form to submit and refresh the page.

但是我现在所拥有的方式似乎无论如何都会刷新.我在模式框上使用了精美的弹出式插件.

But the way I have it now seems it will refresh no matter what. I am using the fancy popup plugin for the modal box.

<form action="submit-comment.php" method="post" id="form">
<input type="text" name="comment" />
<input type="submit" name="submit" value="Submit" />
</form>

jQuery("#form").submit(function() {
     jQuery.fancybox('<div class="box">Some content</div>', {
           'onClosed' : function() { return true; }
            }     
     });
});

如您所见,我试图告诉它仅在关闭时返回true,但实际上并不能正常工作.

As you can see I am trying to tell it to return true only on close but it isn't really working.

有什么想法吗?

推荐答案

尝试一下:

jQuery("#form").submit(function(e) {
     var self = this;
     e.preventDefault();
     jQuery.fancybox('<div class="box">Some amazing wonderful content</div>', {
           'onClosed' : function() { 
                          self.submit();
                        }
     });
     return false; //is superfluous, but I put it here as a fallback
});

javascript是异步的,因此您不能延迟return语句.

javascript is asynchronous so you cannot delay the return statement.

更新

要使其生效以使上面的代码起作用,请将您提交按钮的name更改为其他内容,例如:

To make it so that the code above works, change the name of you submit button to something else, for example:

<input type="submit" name="submit_me" value="Submit" />

这篇关于在提交表单之前执行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆