0

我正在使用 .validity jquery 插件 (validity.thatscaptaintoyou.com/),并尝试使用 .submit,但我不确定如何防止两者同时执行。基本上,除非有效性通过,否则我不想 .submit 执行。

有效性检查:

$("#myform").validity(function() {
    $("#first_name").require();
    $("#last_name").require();
    $("#email").require().match('email', 'Please enter a valid e-mail address');
});

示例 .submit

$('#myform').submit(function(event) {//Execute only if validity passed}
4

2 回答 2

0

您可以尝试使用他们的自定义模式 (function(){

// We'll decide to install our custom output mode under the name 'custom':
 $.validity.outputs.custom = {


    end:function(results) { 


        if (results.valid) {
            $('#myform').submit(function(event) {//Execute only if validity passed});
        }

    }
}
})();

// Now enable the output mode we just installed.
$.validity.setup({ outputMode:'custom' });
于 2013-10-02T16:33:34.657 回答
0

这对我有用:

      function validateMyForm() {

    // Start validation:
    $.validity.start();

    // Validator methods go here:
    $("#first_name").require();
    $("#last_name").require();
    $("#email").require().match('email', 'Please enter a valid e-mail address');

    // All of the validator methods have been called:
    // End the validation session:
    var result = $.validity.end();

    // Return whether it's okay to proceed:
    return result.valid;
}

// Submit handler for /purchase
$('#myform').submit(function(event) {

//Execute only if validity passed
        if (validateMyForm()) {
               //Submit form or do whatever

    }else{
               //Return False so form doesn't actually submit
        return false;
    }
});
于 2013-10-02T23:28:28.030 回答