首先,我是淘汰赛/ javascript的新手,而且我目前正在做一个"未完成"我正在创建一个动态选项"问题是我的ViewModel绑定到我的ItemModel(它包含5个属性HS01 - HS02 - HS03 - HS04 - HS05),当选择OptionsBox时需要绑定。 选中:$ parent。(动态属性)似乎不起作用。
此处的代码示例。
HTML
<input data-bind="value: HS01" />
<table class="table table-condensed">
<tbody data-bind="foreach: HealthAndSafety">
<tr>
<td class="col-xs-9">
<span data-bind="html: contenu"></span>
<!-- ko if: $data.id == 'HS01' -->
<br/>
<ul style="list-style-type: disc">
<!-- ko foreach: $root.HealthAndSafetyChild -->
<li data-bind="text: $data.contenuChild"></li>
<!-- /ko -->
</ul>
<!-- /ko -->
</td>
<!--<div style="border: 3px red dashed;">-->
<td class="col-xs-2">
<input type="radio" data-bind="attr:{value:id,name:group,checked: checkedProperty} , checkedValue: 0"/>
<input type="radio" data-bind="attr:{value:id,name:group,checked: checkedProperty} , checkedValue: 1"/>
</td>
</tr>
</tbody>
</table>
JavaScript(viewmodel)这里是每个UL都有多个Li等...然后它们只有1个是/否单选按钮绑定到父视图模型
m.HealthAndSafety = ko.observableArray([
{ id: 'HS01', group: 'HS01Group', checkedProperty: '$parent.HS01', contenu: f.lang('HealthSafety01') },
{ id: 'HS02', group: 'HS02Group', checkedProperty: '$parent.HS02', contenu: f.lang('HealthSafety02') },
{ id: 'HS03', group: 'HS03Group', checkedProperty: '$parent.HS03', contenu: f.lang('HealthSafety03') },
{ id: 'HS04', group: 'HS04Group', checkedProperty: '$parent.HS04', contenu: f.lang('HealthSafety04') },
{ id: 'HS05', group: 'HS05Group', checkedProperty: '$parent.HS05', contenu: f.lang('HealthSafety05') }]);
m.HealthAndSafetyChild = ko.observableArray([{ idChild: 'HS01_1', contenuChild: f.lang('HealthSafety01_1') }, { idChild: 'HS01_2', contenuChild: f.lang('HealthSafety01_2') }, { idChild: 'HS01_3', contenuChild: f.lang('HealthSafety01_3') }, { idChild: 'HS01_4', contenuChild: f.lang('HealthSafety01_4') }, { idChild: 'HS01_5', contenuChild: f.lang('HealthSafety01_5') }, { idChild: 'HS01_6', contenuChild: f.lang('HealthSafety01_6') }]);
当我发布我的模型更新数据库属性HS01等..是null并且没有绑定。我已经测试了多个案例,但我还没有得到如何将我的ObservableArray(CheckedProperty)绑定到已检查的Property。
我也试过
<input type="radio" data-bind="attr:{value:id,name:group} checked: (checkedProperty / #data.checkedProperty) , checkedValue: 0"/>
我在Stackoverflow上找到的所有示例都绑定到只有1个属性
谢谢!
EDIT1 http://jsfiddle.net/grosjambon/azz3tpfy/7/
EDIT2 找到一种更简洁的方法修复我的问题仍然不是最完整的,但对于我的另一个在html中有多个If条件的方法更好。同时实施罗伊回答
HTML
<div style="border: 3px red dashed;">
<label class="radio-inline">
<input type="radio" data-bind="attr:{name:id}, checked: checkedProperty, checkedValue: '1',event: {change : $root.onInputChangeUpdateVMHSProperty }" />
Yes
</label>
<label class="radio-inline">
<input type="radio" data-bind="attr:{name:id}, checked: checkedProperty, checkedValue: '0' ,event: {change : $root.onInputChangeUpdateVMHSProperty }" />
No
</label>
</div>
的JavaScript
m.onInputChangeUpdateVMHSProperty = function (item) {
switch (item.id) {
case "HS01":
m.HS01(item.checkedProperty());
break;
case "HS02":
m.HS02(item.checkedProperty());
break;
case "HS03":
m.HS03(item.checkedProperty());
break;
case "HS04":
m.HS04(item.checkedProperty());
break;
case "HS05":
m.HS05(item.checkedProperty());
break;
}
};
答案 0 :(得分:2)
主要问题:
checked
属性,请使用checked
绑定checkedProperty
应该是一个可观察的因此,数组的元素将如下所示:
{
id: 'HS01',
group: 'HS01Group',
group2: 'HS01Group2',
checkedProperty: ko.observable(),
contenu: 'test1'
}
您的输入行将是:
<div data-bind="foreach: HealthAndSafety">
<span data-bind="text: $index"></span>
<input data-bind="value: checkedProperty" />
</div>
您的单选按钮如下所示:
<input type="radio" data-bind="attr:{name:id}, checked: checkedProperty, checkedValue: '0'" />
function SeatReservation(name, initialMeal) {
var self = this;
self.name = name;
self.meal = ko.observable(initialMeal);
}
// Here's my data model
var ViewModel = function() {
var self = this;
self.HealthAndSafety = ko.observableArray([{
id: 'HS01',
group: 'HS01Group',
group2: 'HS01Group2',
checkedProperty: ko.observable(),
contenu: 'test1'
}, {
id: 'HS02',
group: 'HS02Group',
group2: 'HS02Group2',
checkedProperty: ko.observable(),
contenu: 'test2'
}, {
id: 'HS03',
group: 'HS03Group',
group2: 'HS03Group2',
checkedProperty: ko.observable(),
contenu: 'test3'
}, {
id: 'HS04',
group: 'HS04Group',
group2: 'HS04Group2',
checkedProperty: ko.observable(),
contenu: 'test4'
}, {
id: 'HS05',
group: 'HS05Group',
group2: 'HS05Group2',
checkedProperty: ko.observable(),
contenu: 'test5'
}]);
};
ko.applyBindings(new ViewModel()); // This makes Knockout get to work
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div>
<h1>
Just testing if the property get binded when u click a radioButton
</h1>
<h1>
Example that works now
</h1>
<div data-bind="foreach: HealthAndSafety">
<span data-bind="text: $index"></span>
<input data-bind="value: checkedProperty" />
</div>
<table>
<tbody data-bind="foreach: HealthAndSafety">
<tr>
<td class="col-xs-2">
<input type="radio" data-bind="attr:{name:id}, checked: checkedProperty, checkedValue: '0'" />
<input type="radio" data-bind="attr:{name:id}, checked: checkedProperty, checkedValue: '1'" />
</td>
</tr>
</tbody>
</table>
</div>