Knockout JS“uniqueName”绑定 - 两个字段同名
我正在使用 Knockout JS 创建一个编辑器。我正在使用 foreach 属性循环模型中的列表。
<tbody data-bind='foreach: Properties'>
我正在使用 JQuery 不引人注目的验证,它需要一个 name 属性来验证。我想为两个字段分配相同的名称,以便能够输出验证消息。是否可以在两个字段上使用相同的 uniqueName 属性?
<tr>
<td>
<input data-bind='value: type, uniqueName: true' data-val = "true", data-val-required = "The Type field is required" /></td>
</td>
</tr>
<tr>
<td class="field-validation-valid" data-valmsg-for="UNIQUENAME" data-valmsg-replace="true"></td>
</tr>
我复制了下面的示例,其中显示了网格编辑和 JQuery 不显眼的验证。但我无法弄清楚如何将验证消息与输入字段链接
http://knockoutjs.com/examples/ gridEditor.html
编辑:
我使用 ASP.NET MVC3 和 Razor 语法作为循环输入。
@Html.DropDownList("Type", new SelectList(types, "Value", "Text"), "Select", new { data_bind = "value: Type", data_val = "true", data_val_required = "The Type field is required" })
我不知道如何更新 name 属性。当我使用 knokcout 添加属性时,它们都具有相同的名称“类型”,并且验证不起作用。它们需要以 Type1 Type2 等方式进行索引。
I am using Knockout JS to create an editor. I am using the foreach property to loop around a list in my model.
<tbody data-bind='foreach: Properties'>
I am using JQuery unobtrusive validation whichneeds a name property to validate. I want to assign the same name to two fields, in order to be able to output a validation message. Is it possible to use the same uniqueName property on two fields?
<tr>
<td>
<input data-bind='value: type, uniqueName: true' data-val = "true", data-val-required = "The Type field is required" /></td>
</td>
</tr>
<tr>
<td class="field-validation-valid" data-valmsg-for="UNIQUENAME" data-valmsg-replace="true"></td>
</tr>
Ive copied the example below which shows grid editing and JQuery unobtrusive validation. But i cant work out how to link the validation message with the input field
http://knockoutjs.com/examples/gridEditor.html
Edit:
I am using ASP.NET MVC3 with Razor syntax for the loop input.
@Html.DropDownList("Type", new SelectList(types, "Value", "Text"), "Select", new { data_bind = "value: Type", data_val = "true", data_val_required = "The Type field is required" })
I cant figure out how to update the name property. When i add a property using knokcout they all have the same name "Type" and the validation doesn't work. They need to be indexed some how Type1 Type2 etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
uniqueName 绑定仅增加索引并设置名称(修复了 IE)。
它看起来像:
因此,您可以创建一个使用最后一个索引并设置适当属性的自定义绑定
现在,您只需使用它,如下所示:
The uniqueName binding just increments an index and sets the name (with a fix for IE).
It looks like:
So, you can create a custom binding that uses the last index and sets the appropriate attribute
Now, you would just use it like:
一种可能更简单的方法源自 RP-Niemeyer 的出色解决方案:
A possibly simpler approach derived from RP-Niemeyer's excellent solution:
我想发表评论,但我的声誉太低了......
非常感谢 RP Niemeyer 的帖子。
我不知道这是否与 requirejs 和淘汰模块有关,但我必须更改
为
现在我有:
I wanted to post as a comment, but my reputation is too low...
Thank you very much RP Niemeyer for your post.
I don't know if this has something to do with requirejs and knockout modules, but I had to change
to
so now I have:
您只需要在子元素中添加 ControlName 属性并将输入名称绑定到此属性以及 data-valms-for 即可
You only need to add a ControlName attribute in the child elements and bind the input name to this and the data-valms-for too