jQuery在DIV上验证插件 [英] jQuery validate plugin on DIV

查看:96
本文介绍了jQuery在DIV上验证插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用div上的验证插件显示在这个问题的答案中:

I am trying to use the validate plugin on a div as shown in the answer to this question:

<script type="text/javascript">
  $("#pseudoForm").validate({
    onfocusout:true,
    rules:{
      first_name:"required",
      last_name:"required"
    }
  });
</script>
<!-- whatever -->
<div id="pseudoForm">
  <input type="text" name="first_name"/>
  <input type="text" name="last_name"/>
</div>

我在表格中都有。

我在不同的浏览器上遇到了一堆不同的错误。

I am getting a bunch of different errors on different browsers.


  • Firefox :未定义的验证程序

  • IE8 :'settings'为null或不是
    对象

  • Chrome :无法读取未定义的属性'设置'

  • Firefox: validator in undefined
  • IE8: 'settings' is null or not an object
  • Chrome: Cannot read property 'settings' of undefined

任何帮助表示感谢!

推荐答案

这不是您想要听到的答案,但另一个答案是错误的(可能是正确的什么时候发布,但从那以后有几个主要的jQuery验证插件更改)。

This isn't the answer you want to hear, but the other answer is incorrect (it may have been right when it was posted, but there have been several major jQuery validation plugin changes since then).

验证插件(目前)设计用于< form> ,以及< form>上的 。您还可以注意到所有插件文档都引用了表单,而不是任何其他通用容器。

The validation plugin is (currently) designed to work on a <form>, and only on a <form>. You can also note that all of the plugin documentation references a form, not any other generic container.

插件本身在内部跟踪 validator.currentForm ,这是指 this 传入的选择器,获取 .elements ,等等...它真的不会以任何其他方式工作,而不是当前版本的编写方式。

The plugin itself keeps track of validator.currentForm internally, which refers to the this of the passed in selector, getting .elements, etc off that...it's really not going to work any other way, not the way the current version's written.

此处的整体解决方案/替代方法:致电 .validate() < form> 元素(相当于jQuery包装器),而不是任何其他元素容器。如果您需要划分< form> ,请使用 < fieldset> 元素,可能使用 ignore:':hidden'选项 .validate()如果您不想验证用户不可见的输入字段。

The overall solution/alternative approach here: call .validate() on the <form> element (the jQuery wrapper for it rather) directly, not any other container. If you need to divide your <form> use <fieldset> elements, possibly using the ignore: ':hidden' option in .validate() if you don't want to validate input fields that aren't visible to the user.

这篇关于jQuery在DIV上验证插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆