我正在玩knockout.js來添加和刪除表單中的字段。到目前爲止它工作正常,但是我需要一個datepicker選項,所以我用jQuery的UI日期選擇器。這是有效的,但僅限於第一個日期選擇器,而不是其他地方。所以每當我點擊'添加'我得到新的領域,但沒有日期選擇器。jQuery UI datepicker with Knockout.js
我使用Google搜索並搜索到StackExchange,但沒有找到複製字段的解決方案。
HTML
<table data-bind='visible: beschikkingen().length > 0'>
<thead>
<tr>
<th>Beschikkingsdatum</th>
<th>Beschikkingsnr</th>
<th />
</tr>
</thead>
<tbody data-bind='foreach: beschikkingen'>
<tr>
<td><input name="beschikkingsdatum[]" type="text" class="beschikkingsdatum" value="" data-bind='value: beschikkingsdatum, uniqueName: true' /> </td>
<td><input class='required number' data-bind='value: beschikkingsnummer, uniqueName: true' /></td>
<td><a href='#' data-bind='click: $root.removebeschikking'>Delete</a></td>
</tr>
</tbody>
</table>
Knockout.JS
var beschikkingModel = function(beschikkingen) {
var self = this;
self.beschikkingen = ko.observableArray(beschikkingen);
self.addbeschikking = function() {
self.beschikkingen.push({
beschikkingsdatum: "",
beschikkingsnummer: ""
});
};
self.removebeschikking = function(beschikking) {
self.beschikkingen.remove(beschikking);
};
self.save = function(form) {
alert("Could now transmit to server: " + ko.utils.stringifyJson(self.beschikkingen));
// To actually transmit to server as a regular form post, write this: ko.utils.postJson($("form")[0], self.beschikkingen);
};
};
var viewModel = new beschikkingModel([
{
beschikkingsdatum: "",
beschikkingsnummer: ""
}
]);
ko.applyBindings(viewModel);
// Activate jQuery Validation
$("form").validate({ submitHandler: viewModel.save });
//]]>
日期選擇器
$(window).load(function(){
$('.beschikkingsdatum').datepicker({
dateFormat: 'dd-mm-yy',
constrainInput: false
});
});
你嘗試使用來自您鏈接的問題結合:HTTP:// stackoverflow.com/questions/6612705/knockout-with-jquery-ui-datepicker –