jQuery 工具验证器,如何使其与动态添加的元素一起工作?
我正在尝试使用 jQuery Tools Validator 来实现表单验证。
我的表单并不简单,它由两个步骤组成:
- 显示初始字段,初始化验证器以确保输入正确的值并填充必填字段。
- 提供所有字段值并单击提交按钮后,我执行 json 请求,并根据结果添加新的输入字段及其相应的验证属性,并取消提交操作。当单击带有新字段的按钮时,就会提交表单。
问题是验证器似乎不知道要验证的新输入。如何刷新它并使验证器了解动态添加的输入?
我尝试过
form.data("validator").destroy();
form.validator();
但没有运气。
有什么建议吗?
I am trying to use jQuery Tools Validator to implement the form validation.
My form isn't simple and it consists of two steps:
- Initial fields are presented, validator is initialized to make sure right values are entered and required fields are filled.
- After all fields values are provided and submit button is clicked, I do json request and depending on the result add new input fields with their corresponding validations attributes and cancel the submit action. When the button is clicked with new fields then the form is submitted.
The thing is the validator doesn't seem to know about the new inputs to validate. how do I refresh it and make validator aware of dynamically added inputs?
I tried
form.data("validator").destroy();
form.validator();
But no luck.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很想使用解除绑定功能的力量来解决问题( http://api.jquery.com/unbind/ )。这个解决方案的好处是我们不必手动将新添加的字段附加到验证器对象。完整的代码如下:
/* server-side.html file */
<代码>
/* server-fail.js 文件 */
您可以尝试此代码以使功能更加动态。享受吧:)
I would love to solve the problem using the power of unbind function( http://api.jquery.com/unbind/ ).The good part of this solution is that we don't have to attach the newly added fields to the validator object manually.The full code is given below:
/* server-side.html file */
/* server-fail.js file */
You can try this code to make the functionality more dynamic.Enjoy :)
嗯,正如我所想,新元素不会附加到现有的验证器对象。
因此,要采取的方法是为新创建的字段初始化验证器,并连接表单的提交事件以从代码执行验证:
这工作得很好。
Well, as I figured new elements are not attached to the existing validator object.
Therefore the way to go was to init validator for new created fields and hook up form's submit event to execute validation from code:
This worked just fine.