Concider是一个jQuery Mobile"列表",包含一个单选按钮和一个标签.
<!-- ko foreach: $data.answers -->
<input type="radio" name="radio-choice" data-bind="attr: { id: [...] }" />
<label data-bind="attr:{ for: [...] }">Label</label>
<!-- /ko -->
Run Code Online (Sandbox Code Playgroud)
为了工作,标签的for属性需要与输入的id相同.
REPLACEMENT FOR [...] RESULTS IN
$index ok
'radio-nr-'+$index fails
$root.testFunction(1) ok
$root.testFunction($index) fails
'radio-nr-'.concat(1) ok
'radio-nr-'.concat($index) fails
Run Code Online (Sandbox Code Playgroud)
哪里
function testFunction(a) { return "radio-nr-"+a; };
Run Code Online (Sandbox Code Playgroud)
为什么我连接$ index的所有尝试都失败了?
谢谢!
与其他绑定上下文属性不同,$ index是一个可观察的
所以你需要$index()在绑定中写(注意括号):
<!-- ko foreach: $data.answers -->
<input type="radio" name="radio-choice"
data-bind="attr: { id: 'radio-nr-' + $index() }" />
<label data-bind="attr:{ for: 'radio-nr-' + $index() }">Label</label>
<!-- /ko -->
Run Code Online (Sandbox Code Playgroud)
JSFiddle中的工作示例
| 归档时间: |
|
| 查看次数: |
2662 次 |
| 最近记录: |