我看到一个非常奇怪的问题。我正在使用ko-js对jQuery移动应用程序中的一些表单字段进行数据绑定。我使用选择和翻转开关成功绑定了所有其他字段,但输入类型=“文本元素不在我的viewmodel中的必填字段上绑定inputText。如果我绑定到viewmodel中的另一个字段,它将被正确显示/更新。< / p>
所以如果与
绑定<input type="text" data-bind="textInput: controlData, attr:{'id': ftId()}" >
我在输入中没有任何值,但如果我与
绑定<input type="text" data-bind="textInput: label, attr:{'id': ftId()}" >
它完美无缺
更全面的HTML摘录:
<div data-role="content" data-theme="a">
<div id="container">
<div data-bind="foreach: formFields">
<div data-role="fieldcontain" data-bind="visible: isFieldText()">
<label data-bind="for: ftId(), text: label()"></label>
<input type="text" data-bind="textInput: controlData, attr:{'id': ftId()}" >
</div>
<div data-role="fieldcontain" data-bind="visible: isFieldCheckbox()">
<label data-bind="for: cbId(), text: label()"></label>
<input type="checkbox" data-role="flipswitch" data-bind="attr: {'id': cbId(), 'data-on-text': dataOnText(), 'data-off-text': dataOffText()}, checked:controlData" /> <!--data-wrapper-class="custom-size-flipswitch"-->
</div>
<div data-role="fieldcontain" data-bind="visible: isFieldSelect()">
<label data-bind="for: selectId(), text: label()"></label>
<select data-bind="attr: {'id': selectId()}, options: options, value: controlData"></select>
</div>
</div>
</div>
</div>
Viewmodel和applyBindings代码
var formFieldViewModel = function (data, target, parent) {
var _this = this;
this.isFieldText = ko.computed(function() {
return _this.controlType() === "text" ? true : false;
});
this.isFieldCheckbox = ko.computed(function() {
return _this.controlType() === "checkbox" ? true : false;
});
this.isFieldSelect = ko.computed(function() {
return _this.controlType() === "select" ? true : false;
});
this.ftId = ko.computed(function() {
return 'ft_'+_this.index();
});
this.cbId = ko.computed(function() {
return 'cb_'+_this.index();
});
this.selectId = ko.computed(function() {
return 'select_'+_this.index();
});
//-- Call mapping function --//
ko.mapping.fromJS(data, target, this);
};
这是JSON对象 - JSON在JSONlint验证
{"formFields":[{"index":0,"label":"experiment description","controlData":"dummy","controlType":"text","dataOffText":"","dataOnText":"","options":[]},{"index":1,"label":"odd S1 Label","controlData":"scottish","controlType":"text","dataOffText":"","dataOnText":"","options":[]},{"index":2,"label":"even S1 Label","controlData":"english","controlType":"text","dataOffText":"","dataOnText":"","options":[]},{"index":3,"label":"use 3P classic","controlData":false,"controlType":"checkbox","dataOffText":"no","dataOnText":"yes","options":[]},{"index":4,"label":"subject","controlData":"National identity","controlType":"select","dataOffText":"","dataOnText":"","options":["Sexuality","Religion","Race \u0026 Culture","National identity","Immigrant culture - football","Gender"]},{"index":5,"label":"country","controlData":"United Kingdom","controlType":"select","dataOffText":"","dataOnText":"","options":["Afghanistan","Albania","Algeria","American Samoa","Andorra","Angola","Anguilla","Antarctica","Antigua and Barbuda","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia","Bosnia and Herzegovina","Botswana","Bouvet Island","Brazil","British Indian Ocean Territory","Brunei Darussalam","Bulgaria","Burkina Faso","Burundi","Cambodia","Cameroon","Canada","Cape Verde","Cayman Islands","Central African Republic","Chad","Chile","China","Christmas Island","Cocos (Keeling) Islands","Colombia","Comoros","Congo","Congo, the Democratic Republic of the","Cook Islands","Costa Rica","Cote D\u0027Ivoire","Croatia","Cuba","Cyprus","Czech Republic","Denmark","Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador","Equatorial Guinea","Eritrea","Estonia","Ethiopia","Falkland Islands (Malvinas)","Faroe Islands","Fiji","Finland","France","French Guiana","French Polynesia","French Southern Territories","Gabon","Gambia","Georgia","Germany","Ghana","Gibraltar","Greece","Greenland","Grenada","Guadeloupe","Guam","Guatemala","Guinea","Guinea-Bissau","Guyana","Haiti","Heard Island and Mcdonald Islands","Holy See (Vatican City State)","Honduras","Hong Kong","Hungary","Iceland","India","Indonesia","Iran, Islamic Republic of","Iraq","Ireland","Israel","Italy","Jamaica","Japan","Jordan","Kazakhstan","Kenya","Kiribati","Korea, Democratic People\u0027s Republic of","Korea, Republic of","Kuwait","Kyrgyzstan","Lao People\u0027s Democratic Republic","Latvia","Lebanon","Lesotho","Liberia","Libyan Arab Jamahiriya","Liechtenstein","Lithuania","Luxembourg","Macao","Macedonia, the Former Yugoslav Republic of","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Martinique","Mauritania","Mauritius","Mayotte","Mexico","Micronesia, Federated States of","Moldova, Republic of","Monaco","Mongolia","Montserrat","Morocco","Mozambique","Myanmar","Namibia","Nauru","Nepal","Netherlands","Netherlands Antilles","New Caledonia","New Zealand","Nicaragua","Niger","Nigeria","Niue","Norfolk Island","Northern Mariana Islands","Norway","Oman","Pakistan","Palau","Palestinian Territory, Occupied","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Pitcairn","Poland","Portugal","Puerto Rico","Qatar","Reunion","Romania","Russian Federation","Rwanda","Saint Helena","Saint Kitts and Nevis","Saint Lucia","Saint Pierre and Miquelon","Saint Vincent and the Grenadines","Samoa","San Marino","Sao Tome and Principe","Saudi Arabia","Senegal","Serbia and Montenegro","Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","South Georgia and the South Sandwich Islands","Spain","Sri Lanka","Sudan","Suriname","Svalbard and Jan Mayen","Swaziland","Sweden","Switzerland","Syrian Arab Republic","Taiwan, Province of China","Tajikistan","Tanzania, United Republic of","Thailand","Timor-Leste","Togo","Tokelau","Tonga","Trinidad and Tobago","Tunisia","Turkey","Turkmenistan","Turks and Caicos Islands","Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","United States","United States Minor Outlying Islands","Uruguay","Uzbekistan","Vanuatu","Venezuela","Viet Nam","Virgin Islands, British","Virgin Islands, U.s.","Wallis and Futuna","Western Sahara","Yemen","Zambia","Zimbabwe"]},{"index":6,"label":"language","controlData":"English","controlType":"select","dataOffText":"","dataOnText":"","options":["Afrikaans","Albanian","Arabic","Armenian","Basque","Bengali","Bulgarian","Catalan","Cambodian","Chinese (Mandarin)","Croation","Czech","Danish","Dutch","English","Estonian","Fiji","Finnish","French","Georgian","German","Greek","Gujarati","Hebrew","Hindi","Hungarian","Icelandic","Indonesian","Irish","Italian","Japanese","Javanese","Korean","Latin","Latvian","Lithuanian","Macedonian","Malay","Malayalam","Maltese","Maori","Marathi","Mongolian","Nepali","Norwegian","Persian","Polish","Portuguese","Punjabi","Quechua","Romanian","Russian","Samoan","Serbian","Slovak","Slovenian","Spanish","Swahili","Swedish","Tamil","Tatar","Telugu","Thai","Tibetan","Tonga","Turkish","Ukranian","Urdu","Uzbek","Vietnamese","Welsh","Xhosa"]},{"index":7,"label":"location","controlData":"Southampton","controlType":"select","dataOffText":"","dataOnText":"","options":["Cardiff","Bournemouth","Upsalla","Rotterdam","Madrid","Southampton","Trondheim","Port Elizabeth","Cape Town","Helsinki","Pisa","Wroclaw","Palermo","Budapest","Malaga","Magaluth","Caerdydd","Glamorgan","Granada","Geneva","Berlin"]}]}