我需要創建一個選項列表,當我單擊一個選項時,我將該值從「X」更改爲關閉所有其他選項。它運行良好,除了我選擇的選項不保持選定狀態。有誰知道什麼是錯的?單擊另一個單選按鈕時更改單選按鈕的值
var data = [{
name: "CheckBox1",
status: " "
}, {
name: "CheckBox2",
status: "X"
}, {
name: "CheckBox3",
status: "X"
}];
var ViewModel = function() {
var self = this;
self.list = ko.mapping.fromJS(data);
self.change = function(data) {
for (i = 0; i < self.list().length; i++)
self.list()[i].status("X");
data.status(" ");
return true;
}
}
ko.applyBindings(new ViewModel(data));
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.min.js"></script>
<!--ko foreach: $root.list-->
<!-- ko with: $data -->
<label data-bind="text: name"></label>
<input type="radio" data-bind="checkedValue: status() === ' ', click:$root.change" />
<!--/ko-->
<!--/ko-->
<!--ko foreach: $root.list-->
<!-- ko with: $data -->
<p data-bind="text: name"></p>
<p data-bind="text: status"></p>
<!--/ko-->
<!--/ko-->
@ Rothth-Nair,非常感謝。 – Guto