1
沒有約束力我有一個函數至極構建HTML控件動態使用jQuery和淘汰賽,但是複選和單選按鈕不起作用,沒有約束力複選和單選按鈕Knockoutjs
請檢查我的代碼,並告訴我一些答案。
function createControls(id, ctrl) {
var obj = $.parseJSON(ctrl.settings)['es'];
var validate = obj.validate;
switch (ctrl.type) {
case 'PlainText':
var PlainText = obj.caption + ": " + "<input data-bind='value: ctrid" + ctrlid + "' onChange='javascript:saveValue(" + id + ",this.value)' value='" + obj.value + "'><br>";
BuildViewModel(obj);
return PlainText;
break;
case 'PasswordEdit':
var PasswordEdit = obj.caption + ": " + "<input type='password' data-bind='value: ctrid" + ctrlid + "' value='" + obj.value + "'><br>";
BuildViewModel(obj);
return PasswordEdit;
break;
case 'SingleLineText':
var single = obj.caption + ": " + "<input data-bind='value: ctrid" + ctrlid + "' name='ctrid" + ctrlid + "' onChange='javascript:saveValue(" + id + ",this.value)' value='" + obj.value + "' required='" + validate.required + "' ><br>";
validar(validate);
BuildViewModel(obj);
return single;
break;
case 'OptionMultiple':
var array_js = new Array();
var i = 0;
var check = "<div align='left'><br>" +"<label>"+ obj.caption + ": " + "</label>";
$.each(obj.items, function (k, v) {
check += "<input type='checkbox' value='" + v.value + "' data-bind='checked: ctrlid" + ctrlid + "'>" + v.value;
array_js[i] = v.value;
i++
});
check += "</div>"
BuildViewModelCheck(obj, array_js);
return check;
break;
case 'SingleCombo':
var array_js = new Array();
var i = 0;
var combo = "<div align='left'><br>" + "<label>" + obj.caption + ": " + "</label>";
combo += "<select name='ctrid" +ctrlid + "' data-bind='options: ctrid" + ctrlid + ",value:select" + ctrlid + "'></select>";
combo += "</div>";
$.each(obj.items, function (k, v) {
array_js[i] = v.value;
i++
});
validar(validate);
BuildViewModelCombo(obj, array_js);
return combo;
break;
case 'Option':
var array_js = new Array();
var i = 0;
var option = "<div align='left'><br>" + "<label>" + obj.caption + ": " + "</label>";
$.each(obj.items, function (k, v) {
option += "<input type='radio' value='" + v.value + "' data-bind='checked: ctrlid" + ctrlid + "' name='ctrid" + ctrlid + "'/>" + v.value;
array_js[i] = v.value;
i++
});
option += "</div>"
validar(validate);
BuildViewModelRadio(array_js);
return option;
break;
}
}
function BuildViewModel(obj) {
var iden = "ctrid" + ctrlid;
viweModel[iden] = ko.observable(obj.value);
}
function BuildViewModelCheck(obj,array) {
var iden = "ctrid" + ctrlid;
viweModel[iden] = ko.observable(true);
//ko.observableArray(["Esposa", "Hijos"]);
}
function BuildViewModelRadio(aray) {
var iden = "ctrid" + ctrlid;
viweModel[iden] = ko.observable(aray);
}
function BuildViewModelCombo(obj, array) {
var iden = "ctrid" + ctrlid;
var select= "select" + ctrlid;
viweModel[iden] = array;
viweModel[select] = ko.observable();
}
當你叫ko.applyBindings?如果這些控件是在該點之後創建的,那麼在添加它之後,您需要調用'ko.applyBindings(data,yourNewElementWithBindings)'。似乎你可能會考慮使用模板,因爲它會自動處理。 –
ummm讓我試試!!!,謝謝 –
對不起,Sean,打電話給ko.applyBindings是在一個html文件中 –