javascript jquery.validate 不显示消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21396307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:49:31  来源:igfitidea点击:

jquery.validate not showing messages

javascriptjquery

提问by isaac weathers

Can't seem to find out why my jquery.validate script is not showing the messages. Anyone see what I am doing wrong?

似乎无法找出为什么我的 jquery.validate 脚本没有显示消息。有人看到我做错了什么吗?

Trying to display messages in a separate div:

尝试在单独的 div 中显示消息:

html:

html:

  <form class="cmxform" id="commentForm" method="get" action="">
                <label for="vehiclePrice" class="form-control-label vpl">Vehicle Price</label>
                <input type="text" class="form-control" id="vehiclePrice" placeholder="
$(document).ready(function() {

$('#commentForm').validate({
    errorLabelContainer: "#error-note",
    wrapper: "li",
    onfocusout: function(element, event) {
        this.element(element);
    },
    rules: {
        vehiclePrice: 'required',
        estimatedTaxesAndFees: 'required'

    },
    messages: {
        vehiclePrice: 'Please enter vehicle price',
        estimatedTaxesAndFees: 'Please enter taxes and fees
    }
});

});
" onkeypress="return isNumberKey(event)" value="25592" required /> <label for="estimatedTaxesAndFees" class="form-control-label etfl">Estimated Taxes and Fees</label> <input type="text" class="form-control" id="estimatedTaxesAndFees" placeholder="
<input type="submit" value="Submit" />
" onkeypress="return isNumberKey(event)" value="2843" required /> </form> <div id="error-note"></div>

js:

js:

estimatedTaxesAndFees: 'Please enter taxes and fees' // <-- Here

fiddle

小提琴

回答by Felix

1) Include jQuery in your fiddle

1) 在你的小提琴中包含 jQuery

2) Add submit button to your HTML:

2) 将提交按钮添加到您的 HTML 中:

<input type="text" class="form-control" name="vehiclePrice" id="vehiclePrice"
<input type="text" class="form-control" name="estimatedTaxesAndFees"

3) Add missing 'at the end of your estimatedTaxesAndFeesmessage:

3)'在您的estimatedTaxesAndFees消息末尾添加缺失:

$(document).ready(function() {

    $('#commentForm').validate({
        errorLabelContainer: "#error-note",
        wrapper: "li",
        onfocusout: function(element, event) {
            this.element(element);
        },
        rules: {
            vehiclePrice: 'required',
            estimatedTaxesAndFees: 'required'

        },
        messages: {
            vehiclePrice: 'Please enter vehicle price',
            estimatedTaxesAndFees: 'Please enter taxes and fees'//closing ' was missing
        }
    });

});

Updated Fiddle

更新的小提琴



You need to add nameattribute to your inputto get custom message here:

您需要在此处添加name属性input以获取自定义消息:

##代码##

Updated Fiddle

更新的小提琴

回答by Arun P Johny

you had a unclosed string literal, also in the fiddle jQuery was not included

你有一个未关闭的字符串文字,也在小提琴中不包括 jQuery

##代码##

Demo: Fiddle

演示:小提琴

The validation will trigger if you clear the contents of a textbox/or you add a submit button to the form/or call the valid()method of the form manually

如果您清除文本框的内容/或向表单添加提交按钮/或valid()手动调用表单的方法,将触发验证