jquery新手:将验证与隐藏提交按钮结合起来
我是 jQuery 新人。我已经验证了我的表单(MVC 1.0 / C#)的工作方式:
<script type="text/javascript">
if (document.forms.length > 0) { document.forms[0].id = "PageForm"; document.forms[0].name = "PageForm"; }
$(document).ready(function() {
$("#PageForm").validate({
rules: {
SigP: { required: true }
},
messages: {
SigP: "<font color='red'><b>A Sig Value is required. </b></font>"
}
});
});
</script>
我还想隐藏“提交”按钮,以防止抽搐鼠标综合症在控制器完成和重定向之前导致重复输入(我正在使用 GPR 模式) 。以下内容可用于此目的:
<script type="text/javascript">
//
// prevent double-click on submit
//
jQuery('input[type=submit]').click(function() {
if (jQuery.data(this, 'clicked')) {
return false;
}
else {
jQuery.data(this, 'clicked', true);
return true;
}
});
</script>
但是,我无法让两者一起工作。具体来说,如果单击“提交”按钮后验证失败(考虑到表单的工作方式,就会发生这种情况),那么我将无法再次提交表单,除非我执行浏览器刷新来重置“已单击”属性。
如何重写上面的第二种方法,除非表单验证,否则不设置 clicked 属性?
谢谢。
I'm new a jQuery. I have gotten validate to work with my form (MVC 1.0 / C#) with this:
<script type="text/javascript">
if (document.forms.length > 0) { document.forms[0].id = "PageForm"; document.forms[0].name = "PageForm"; }
$(document).ready(function() {
$("#PageForm").validate({
rules: {
SigP: { required: true }
},
messages: {
SigP: "<font color='red'><b>A Sig Value is required. </b></font>"
}
});
});
</script>
I also want to hide the Submit button to prevent twitchy mouse syndrome from causing duplicate entry before the controller completes and redirects (I'm using an GPR pattern). The following works for this purpose:
<script type="text/javascript">
//
// prevent double-click on submit
//
jQuery('input[type=submit]').click(function() {
if (jQuery.data(this, 'clicked')) {
return false;
}
else {
jQuery.data(this, 'clicked', true);
return true;
}
});
</script>
However, I can't get the two to work together. Specifically, if validate fails after the Submit button is clicked (which happens given how the form works), then I can't get the form submitted again unless I do a browser refresh that resets the 'clicked' property.
How can I rewrite the second method above to not set the clicked property unless the form validates?
Thx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于那些像我一样仍在寻找答案的人,我是这样做的:
For those still looking for an answer like I was, this is how I did it:
如果您使用此验证插件,它带有一个 valid() 方法。
甚至:
If you're using this validation plugin, it comes with a valid() method.
or even:
我会这样做:
这是在提交表单时触发的,查找内部的任何提交按钮,如果表单有效则禁用它。
准备就绪后,使用
$('#PageForm :submit').removeAttr('disabled');
重新启用该按钮。I would do it like this:
This is triggered on submit of the form, looks for any submit button inside and disables it if the form was valid.
Use
$('#PageForm :submit').removeAttr('disabled');
to re-enable the button when ready.