knockoutjs '购物车编辑器' jquery 验证插件问题的示例
我在 http://knockoutjs.com/examples/cartEditor.html (jsFiddle - http://jsfiddle.net/rniemeyer/adNuR/) 并添加了 jquery 验证插件。我想根据第一个 Category
选择列表进行验证以使其成为必需。所以我做了以下更改:
- 使提交按钮只是一个直接的
type=submit
输入, - 包围
- 执行
$("form" ).validate()
在脚本末尾 - 添加了
class='required'
在 - 添加了
uniqueName: true
在数据绑定
这是我制作的jsFiddle: http://jsfiddle.net/niner/JXJtj/3/ 。因此,如果 category
列表中没有任何内容,验证就会起作用。但是,如果我做出有效选择(即摩托车),当我按“提交”时,它仍然显示“该字段为必填项”。即使我正确选择了所有其他选项,我仍然在 category
选择控件上收到验证错误。有人可以看一下并让我知道我在这里缺少什么吗?
谢谢。
I took the original 'Cart editor' example at http://knockoutjs.com/examples/cartEditor.html (jsFiddle - http://jsfiddle.net/rniemeyer/adNuR/) and added jquery validation plugin into it. I want to validate against the 1st Category
select list to make it required. So I made the the following changes:
- made submit button just a straight
type=submit
input - surrounded the controls in a
<form>
- execute
$("form").validate()
at the end of script - added
class='required'
in the<select>
- added
uniqueName: true
indata-bind
Here's the jsFiddle I made: http://jsfiddle.net/niner/JXJtj/3/. So the validation will work if the category
list has nothing in it. However, if I make a valid selection (i.e. Motorcycles), when I press 'submit', it still says 'The field is required.'. Even if I selected all the other choices correctly, I still get validation error on the category
select control. Can someone please take a look and let me know what I'm missing here?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的选项都没有值,因此验证总是失败,因为
.val()
始终返回空字符串。您可以通过将optionsValue: "name"
添加到data-bind
表达式来为您的选项指定值:http://jsfiddle.net/gilly3/JXJtj/4/
None of your options have a value, so validation always fails because
.val()
always returns an empty string. You can give your options a value by addingoptionsValue: "name"
to yourdata-bind
expression:http://jsfiddle.net/gilly3/JXJtj/4/