我在綁定上做了很多工作。現在即時通訊嘗試通過jQuery創建一個元素創建。我的問題是當我使用jquery創建了一個新的元素,並且它沒有與knockout進行交互。請幫我:(我想這應該是重新綁定.....KnockoutJs - 數據綁定不能在新元素上工作
當我點擊的jQuery它不工作:(
創建的添加按鈕這是我的代碼: HTML
User List:<br>
<table>
<thead><tr>
<th>name</th><th>action</th>
</tr></thead>
<tbody class="user-list">
<tr>
<td>anthony</td>
<td><input type="button" data-bind="click: addUser" value="add"/></td>
</tr>
</tbody>
</table>
<input type="button" class="btnAdd" value="add User"/>
User to Block:<br>
<table>
<thead><tr>
<th>Username</th>
</tr></thead>
<tbody data-bind="foreach: users">
<tr>
<td><input data-bind="value: name" /></td>
</tr>
</tbody>
</table>
我的JS:對提前
$(".btnAdd").bind('click',function(){
$('.user-list').append('<tr><td>joey</td> <td><input type="button" data-bind="click: addUser" value="Add"/></td></tr> ');});
function UserList(name) {
var self = this;
self.name = name;
}
function UserViewModel() {
var self = this;
self.users = ko.observableArray();
self.addUser = function() {
self.users.push(new UserList("it works"));
}
}
ko.applyBindings(new UserViewModel());
感謝
非常感謝你!這解決了我的問題! :) –