简体   繁体   English

jQuery的validator.addMethod

[英]jquery validator.addMethod

I'm using a swap value script to put the field label inside the input, on focus it clears and on blur it keeps the value you entered. 我正在使用交换值脚本将字段标签放在输入中,在清除时会保持焦点,而在模糊时会保留您输入的值。 I need to make a validator.addMethod so it won't validate with its initial value. 我需要制作一个validator.addMethod,因此它不会使用其初始值进行验证。 Here's the swap value script 这是交换价值脚本

$(function() {
        swapValues = [];
        $(".swap_value").each(function(i){
            swapValues[i] = $(this).val();
            $(this).focus(function(){
                if ($(this).val() == swapValues[i]) {
                    $(this).val("");
                }
            }).blur(function(){
                if ($.trim($(this).val()) == "") {
                    $(this).val(swapValues[i]);
                }
            });
        });
    });

and the validator.addMethod for a text input who's value/label is "First Name" 以及值/标签为“名字”的文本输入的validator.addMethod

$.validator.addMethod(
            "Foo",
            function (value, element) {
                if ($("#firstName").val === "First Name") {
                return false;
            } else return true;
        },
        "First name required"
    );

With this, when you enter in the text box and enter a different value its fine, when you exit the text box it reverts to the initial value. 这样,当您在文本框中输入并输入其他值时,就可以了,当您退出文本框时,它会恢复为初始值。 Is this due to my swap value routine or the validator.addMethod? 这是由于我的交换值例程还是Validator.addMethod? when I remove the validator.addMethod it works fine. 当我删除validator.addMethod时,它工作正常。 thanks, 谢谢,

I'm not sure what effect this will have on your problem, but in your addMethod function you have: 我不确定这会对您的问题产生什么影响,但是在您的addMethod函数中,您有:

if ($("#firstName").val === "First Name")

but you should have: 但您应该具有:

if ($("#firstName").val() === "First Name")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM