Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery对";标签输入“;插件_Jquery_Jquery Validate - Fatal编程技术网

Jquery对";标签输入“;插件

Jquery对";标签输入“;插件,jquery,jquery-validate,Jquery,Jquery Validate,我正在利用流行的方法来验证我的表单。以我正在使用的相同形式 我能够验证表单上的所有表单字段,除了“标签输入”插件正在使用的字段 原因是,原来的输入被隐藏,新的输入由插件绘制 将我的验证样式应用于标记输入的任何帮助都将不胜感激,Thanx默认情况下jQuery validate会忽略不可见的字段。若要覆盖此行为,请使用$(“#someForm”).validate({ignore:”“})两个问题: 所有字段都必须有名称。如果属性不存在,验证将无法正常运行 您必须验证案例中的隐藏字段。您要做的是

我正在利用流行的方法来验证我的表单。以我正在使用的相同形式

我能够验证表单上的所有表单字段,除了“标签输入”插件正在使用的字段

原因是,原来的输入被隐藏,新的输入由插件绘制


将我的验证样式应用于标记输入的任何帮助都将不胜感激,Thanx默认情况下jQuery validate会忽略不可见的字段。若要覆盖此行为,请使用
$(“#someForm”).validate({ignore:”“})

两个问题:

  • 所有字段都必须有
    名称
    。如果属性不存在,验证将无法正常运行
  • 您必须验证案例中的隐藏字段。您要做的是使用
    ignore:[]
    对隐藏字段进行验证
  • 总之,您的代码应该如下所示:

    HTML:

    <form id="someForm" name="someForm" method="post">
        <input type="text" name="required-field" class="required"/>
        <input id="tagsx" type="text" name="tags" class="required" />
        <input type="submit" value="submit"/>
    </form>
    
    $(document).ready(function () {
        $("#someForm").validate({
            ignore: []
        });
        $('#tagsx').tagsInput({
            width: 'auto',
            autocomplete_url: 'http://xoxco.com/projects/code/tagsinput/test/fake_json_endpoint.html'
        });
    });
    
    示例:


    请注意,您必须使用
    errorPlacement
    或类似工具才能正确显示错误消息。

    验证插件的默认设置为
    ignore::hidden“
    ,它使用
    jQuery.not()
    从ignore选项中排除所有内容

    如果要包含隐藏字段,可以使用以下选项:

    $("#myform").validate({
       ignore: ""
    });
    

    在您的情况下,请使用以下Javascript代码:

    $(document).ready(function () {
        $("#someForm").validate({
           ignore: ""
        });
        $('#tagsx').tagsInput({
            width: 'auto',
            autocomplete_url: 'http://xoxco.com/projects/code/tagsinput/test/fake_json_endpoint.html'
        });
    });
    

    更新:正如我在验证代码中看到的那样,
    名称
    -属性是非常必需的,因为插件用
    名称
    存储所有内容。即使只在代码中添加
    ignore:“
    ,它也会给出一个验证错误,因为它使用相同的
    名称存储,并且名称为
    null

    下面是一个正在工作的JSFIDLE:


    您可以使用一个自定义方法验证一个虚拟文本字段,该方法将验证您的标记字段

    HTML: JavaScript:
    但是,我喜欢这种方法,为了让赏金公平,您能启动并运行一个JSFIDLE示例吗?这是一个很好的答案。有点像jquery.validate的“缺失手册”
    <form id="someForm" name="someForm" method="post">
        <input type="text" id="txt1" name="txt1"/>
        <input id="tagsx" name="tagsx" type="text" />
        <input id="dummy" name="dummy" type="text" /><!-- dummy text field -->
        <input type="submit" value="submit"/>
    </form>
    
    /*To hide dummy text field*/
    #dummy{
        visibility: hidden!Important;
        height:1px;
        width:1px;
    }
    
    $(document).ready(function () {
    
        $.validator.addMethod("checkTags", function(value) { //add custom method
            //Tags input plugin converts input into div having id #YOURINPUTID_tagsinput
            //now you can count no of tags
            return ($("#tagsx_tagsinput").find(".tag").length > 0);
        });
    
        $("#someForm").validate( {
            rules: {
                txt1:"required",
                dummy:"checkTags"
            },
            messages: {
                txt1: "Required",
                dummy: "Required"
            }
        });
    
        $('#tagsx').tagsInput({
            width: 'auto',
            autocomplete_url: 'http://xoxco.com/projects/code/tagsinput/test/fake_json_endpoint.html' 
        });
    });