【问题标题】:How to use .submit with .validity如何使用 .submit 和 .validity
【发布时间】:2013-10-03 07:28:28
【问题描述】:

我正在使用 .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}

【问题讨论】:

    标签: jquery validity.js


    【解决方案1】:

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

    // 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' });
    

    【讨论】:

    • 我试过了,但它只是提交而没有通过有效性进行任何验证,或者在“end:”中运行.submit。我还尝试添加 raise: 和 raiseAggregate: 选项,但这会导致表单提交的结果相同。
    【解决方案2】:

    这对我有用:

          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;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多