0

我有一个非常简单的表单,其中包含几个字段和几个复选框。我需要的是当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>
4

2 回答 2

0

将要触发的处理程序放在form.submit(). 它将在表单实际提交到网络服务器之前执行。

见:http ://api.jquery.com/submit/

.submit()是一个事件,就像单击、悬停等一样。您可以将处理程序绑定到它将在默认操作之前执行。

于 2012-09-14T09:20:00.327 回答
0

查看您的代码,我认为您应该在

form.submit() 

在 submitHandler 内部调用,所以它应该变成

submitHandler: function(form) {
    //your call goes here...
    form.submit();  
 }
于 2012-09-14T09:29:23.087 回答