JQuery 验证未捕获的类型错误:无法读取未定义的属性“设置”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27509958/
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-08-26 11:25:09  来源:igfitidea点击:

JQuery Validate Uncaught TypeError: Cannot read property 'settings' of undefined

jqueryjquery-mobilejquery-validate

提问by user3446526

I am using JQuery Validate on a JQuery Mobile form that has a listview with an Auto Complete option that is created dynamically.

我在 JQuery Mobile 表单上使用 JQuery 验证,该表单具有一个带有动态创建的自动完成选项的列表视图。

I get this error if you try and type into the auto complete field, once validation is active.

一旦验证处于活动状态,如果您尝试在自动完成字段中键入内容,我会收到此错误。

I can see that the problem is related to the form that jQuery Mobile creates to hold the filter input but I can not figure out how to stop jQuery Validate from validating the field.

我可以看到问题与 jQuery Mobile 创建的用于保存过滤器输入的表单有关,但我无法弄清楚如何阻止 jQuery Validate 验证该字段。

I can reproduce it using JS Fiddle

我可以使用JS Fiddle重现它

(Press Validate and then type in the filter box)

(按验证,然后在过滤器框中键入)

Any suggestions appreciated

任何建议表示赞赏

Thanks

谢谢

<body>
<script>
    jQuery.validator.setDefaults({
        ignore: '[data-type="search"]'

    });
</script>
<form>
    <input required>
    <ul id="ac" data-inset="true" data-filter="true" data-filter-reveal="true" data-filter-placeholder="Search cars...">
        <li><a href="#">Acura</a>

        </li>
        <li><a href="#">Audi</a>

        </li>
    </ul>
    <button onclick="$('#ac').attr('data-role','listview');$('#ac').listview();$('form').validate();">Validate</button>
</form>

回答by Sparky

Your whole problem is being cause by the dynamic HTML that is created by the listview()function. Inspecting the DOM reveals that this dynamically creates a new <form>container. Since you've placed listviewwithin your <form>container, you now have <form></form>nested within <form></form>, which is very invalid HTML, and the whole reason the jQuery Validate plugin is working so unexpectedly.

您的整个问题都是由listview()函数创建的动态 HTML 引起的。检查 DOM 会发现这会动态创建一个新<form>容器。由于您已放置listview<form>容器中,因此现在<form></form>嵌套在 中<form></form>,这是非常无效的 HTML,这也是 jQuery Validate 插件出乎意料地工作的全部原因。

The solution is to place your listview element outside of your <form>container.

解决方案是将 listview 元素放在<form>容器之外。