简体   繁体   English

防止jQuery验证插件在可见时提交表单

[英]Preventing jQuery validation plugin from submitting form when made visible

I would like to validate a form using the http://jqueryvalidation.org/ plugin. 我想使用http://jqueryvalidation.org/插件来验证表单。 But instead of submitting the form, I wish to first display the results so that the user could make extra sure and then submit the data. 但是,我不想提交表单,而是希望首先显示结果,以便用户可以更加确定,然后提交数据。 The "extra sure" page has a back button which just displays the original form, but when the form is shown, it seems to instantly trigger the submit callback. “额外确定”页面上有一个后退按钮,该按钮仅显示原始表单,但是当显示该表单时,它似乎立即触发了提交回调。 Please see script below and a live example at http://jsfiddle.net/SF9R4/ . 请参阅下面的脚本和位于http://jsfiddle.net/SF9R4/的实时示例。 How do I prevent this behavior? 如何防止这种行为?

var validator=$("#my-form").validate({
    rules: {name:{required:true},address:{required:true},city:{required:true}},
    submitHandler: function(form) {
        //Populate #my-form 
        $('#my-form>div').toggle();
    }
});

$('#go-back').click(function(){
    $('#my-form>div').toggle();
    alert('go-back');
});

$('#submit').click(function(){
    alert('Submit Form');
});

<form method="post" action="myform.php" id="my-form">
    <div class="step1">
        <input type="text" name="name" id="name" />
        <input type="text" name="address" id="address" />
        <input type="text" name="city" id="city" />
        <input type="submit" name="submit" value="Submit">
    </div>
    <div class="step2 hidden">
        <dl class="info">
            <dt>Name</dt><dd id="name"></dd>
            <dt>Address</dt><dd id="address"></dd>
            <dt>City</dt><dd id="city"></dd>
        </dl>
        <button id='go-back'>Go Back</button>
        <button id='submit'>Complete</button>
    </div>
</form>

Add a button type. 添加按钮类型。 http://jsfiddle.net/SF9R4/1/ http://jsfiddle.net/SF9R4/1/

<button id='go-back' type="button">Go Back</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM