入门
监控属性
使用内置绑定
控制文本和外观
绑定逻辑控制
处理表单属性
click
绑定event
绑定submit
绑定enable
绑定disable
绑定value
绑定hasfocus
绑定checked
绑定options
绑定selectedOptions
绑定uniqueName
绑定
解析模板
高级应用
插件
更多信息
"uniqueName" 绑定
目的
uniqueName
绑定确保所绑定的元素有一个非空的 name
属性。如果该元素没有 name
属性,那绑定会给它设置一个unique的字符串值作为name属性。
The uniqueName
binding ensures that the associated DOM element has a nonempty name
attribute. If the DOM element did not have a name
attribute, this binding gives it one and sets it to some unique string value.
你不会经常用到它,只有在某些特殊的场景下才用到,例如:
You won’t need to use this often. It’s only useful in a few rare cases, e.g.:
在使用KO的时候,一些技术可能依赖于某些元素的name属性,尽快他们没有什么意义。例如, jQuery Validation 验证当前只验证有name属性的元素。为配合Knockout UI使用,有些时候需要使用
uniqueName
绑定避免让jQuery Validation验证出错。 请参考 在KO中使用jQuery Validation.Other technologies may depend on the assumption that certain elements have names, even though names might be irrelevant when you’re using KO. For example, jQuery Validation currently will only validate elements that have names. To use this with a Knockout UI, it’s sometimes necessary to apply the
uniqueName
binding to avoid confusing jQuery Validation. See an example of using jQuery Validation with KO.IE 6下,如果radio button没有
name
属性是不允许被checked了。大部分时候都没问题,因为大部分时候radio button元素都会有name
属性的作为一组互相的group。不过,如果你没声明,KO内部会在这些元素上使用uniqueName
那么以确保他们可以被checked。IE 6 does not allow radio buttons to be checked if they don’t have a
name
attribute. Most of the time this is irrelevant because your radio button elements will have name attributes to put them into mutually-exclusive groups. However, just in case you didn’t add aname
attribute because it’s unnecessary in your case, KO will internally useuniqueName
on those elements to ensure they can be checked.
例子
<input data-bind="value: someModelProperty, uniqueName: true" />
参数
- 主参数
就像上面的例子一样,传入 true
(或者可以转成true的值)以启用 uniqueName
绑定。
Pass true
(or some value that evaluates as true) to enable the uniqueName
binding, as in the preceding example.
其他参数
- 无
依赖性
除KO核心类库外,无依赖。
(c) knockoutjs.com