我有一个非常简单的表单,包含几个字段和几个复选框。我需要的是当选中postto
复选框时,它会在提交之前调用一个函数。
我怎么能这样做?
$("#form").validate({
debug: true,
errorContainer: $(".warning"),
rules: {
comment: {
maxlength: 25
},
'phone': {
number: true
}
},
submitHandler: function(form) {
form.submit();
}
});
<form id="form" method="post">
<div class="first"><textarea name="comment" id="comment" tabindex="1" placeholder="Write your fondest memory here *" required></textarea></div>
<div><label for="firstname">First Name *</label><input name="firstname" type="text" id="firstname" tabindex="2" required></div>
<div><label for="lastname">Last Name *</label><input name="lastname" type="text" id="lastname" tabindex="3" required></div>
<div><label for="email">Email Address *</label><input name="email" type="email" id="email" tabindex="4" required></div>
<div><label for="phone">Phone Number *</label><input name="phone" type="text" id="phone" tabindex="5" required></div>
<section>
<div class="postto"><input name="postto" type="checkbox" id="postto" value="1" tabindex="6"><label for="postto">Post to my wall</label></div>
<div class="newsletter"><input name="newsletter" type="checkbox" id="newsletter" value="1" tabindex="7"><label for="newsletter">I want to sign up to the Penguin newsletter</label></div>
<div class="submit-button"><input name="submitbutton" type="submit" value="enter" tabindex="8"></div>
<p class="warning">^ Please fill in the required fields with the correct information.</p>
</section>
</form>
答案 0 :(得分:0)
将您要处理的处理程序放在form.submit()
上。它将在表单实际提交给Web服务器之前执行。
请参阅:http://api.jquery.com/submit/
.submit()
是一个事件,就像点击,悬停等一样。您可以将处理程序绑定到将在默认操作之前执行的处理程序。
答案 1 :(得分:0)
查看你的代码,我认为你应该在
之前调用你的函数form.submit()
在submitHandler中调用,因此它应该成为
submitHandler: function(form) {
//your call goes here...
form.submit();
}