0

我的网站上有一个表单,当我单击提交时,我有一个检查空字段的验证脚本。我的表单中只有一个字段不是必需的,因此我尝试将其添加为例外。这是我当前的脚本

$("#quote_form").submit(function(){
    var isFormValid = true;

    $("input, select").not('.discount').each(function() {
        if ($.trim($(this).val()).length == 0){
            $(this).addClass("highlight");
            isFormValid = false;
        }
        else{
            $(this).removeClass("highlight");
        }
    });

    if (!isFormValid) {
        $('.error').text('**Please fill in all highlighted fields**');
    }
    else {
        $('.error').text('');
    }

    return isFormValid;
});

这样做的原因是,当我单击提交时,它不会将“突出显示”类添加到具有“折扣”类的字段但是,当我填写除折扣以外的所有表单字段时,它仍然不会发送。谁能告诉我我错过了什么?

4

1 回答 1

0

I would go for a more complete and simpler approach: use jquery.validate.

With this widely used plugin you can manage validation rules either by code or (suggested!) by css classes and attributes directly applied to your inputs (i.e. required, email, min/max length, min/max value, etc).

Check here for few usage examples.

Note that error messages and corresponding localizations are already included.

于 2013-11-30T15:49:42.190 回答