如果在 css class 上无效,则阻止提交表单

prevent form from submitting if invalid on css class

您好,我正在使用 jQuery 验证,但是由于代码中的限制,我正在尝试对特定的 css 类 进行验证,我试过这个:

    $.validator.setDefaults({
        submitHandler: function (form) {
            alert('submitted'); 
            form.submit();
        }
    });

    $('#shopLeft .cart').validate();


    $('.addon-wrap-7479-basics-0 .addon-custom').rules('add', {
        required: true,
        minlength: 2,
        maxlength: 20,
    });


    $('.addon-wrap-7479-description-2 .addon-custom-textarea').rules('add', {
        required: true,
        minlength: 2,
        maxlength: 20,
    });


    $('.addon-wrap-7479-upload-3 .addon-custom').rules('add', {
        required: true,
        minlength: 2,
        maxlength: 20,
    });


    $('.product-addon-nameproject .addon-custom').rules('add', {
        required: true
    });

谁能帮忙。

You should know that the methods of the jQuery Validate plugin only work on the FIRST matched element when the selector contains multiple elements.

我在你的代码中看到了很多 class 选择器,所以我想知道你是否试图为每个 .validate() 和 [=13= 的实例定位一个以上的元素].如果是这样,你还需要一个 jQuery .each().

// more than one form with class="cart"
$('#shopLeft .cart').each(function() { // more than one form with class="cart"
    $(this).validate();
});


// more than one field with class="addon-custom" within a class="addon-wrap-7479-basics-0"
$('.addon-wrap-7479-basics-0 .addon-custom').each() {
    $(this).rules('add', {
        required: true,
        minlength: 2,
        maxlength: 20,
    });
});