我的頁面如下:在observableArray
<button id="add">Add Data</button>
<button id="show">show</button>
<table>
<tr style="vertical-align:top">
<td>
<table border="1">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<tbody data-bind="foreach: students">
<tr>
<td data-bind="text: id"></td>
<td>
<input type="text" data-bind="value: name" />
</td>
<td> <a href="javascript:void(0)" data-bind="click: $root.showData">Select</a>
</td>
</tr>
</tbody>
</table>
</td>
<td>
<table id="data">
<tbody data-bind="with: selectedData">
<tr>
<td>Id</td>
<td>
<input type="text" data-bind="value: id" />
</td>
</tr>
<tr>
<td>Name</td>
<td>
<input type="text" data-bind="value: name" />
</td>
</tr>
<tr>
<td></td>
<td>
<input type="button" value="Close" />
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
的JavaScript如下:
function ViewModel() {
var self = this;
self.students = ko.observableArray([]);
self.showData = function (dt) {
if (window.console) console.log(dt);
self.selectedData(dt);
$('#data').show();
}
this.selectedData = ko.observable();
}
$(function() {
window.appViewModel = new ViewModel();
ko.applyBindings(window.appViewModel);
$('#add').click(function() {
var model = window.appViewModel;
$.each(students, function (idx, student) {
if (window.console) console.log(student);
model.students.push(student);
});
$('table').show();
});
$('table').hide();
$('input').click(function() {
$('#data').hide();
});
$('#show').click(function() {
var s = JSON.stringify(window.appViewModel.students());
alert(s);
});
});
前瞻:
在圖片中,我點擊與id = 3的學生相對應的選擇。另一張表顯示爲t他選擇了學生的細節。假設我在文本框1中輸入了一些內容,文本框2不更新,反之亦然。
如何做到這一點?
小提琴:http://jsfiddle.net/deostroll/YdrQf/1/