获取input绑定的未捕获错误“NO_MODIFICATION_ALLOWED_ERR”

我从这开始:

<script src="/Scripts/jquery-1.6.2.min.js" ... <script src="/Scripts/knockout-1.2.1.debug.js" ... <script src="/Scripts/knockout.mapping-latest.js" ... <script src="/Scripts/jquery.unobtrusive-knockout.min.js" ... 

然后我从服务器拉一个扁平的JSON对象,并将每个find的属性绑定到DOM中的匹配元素:

 $.ajax({ url: '/GetRecord', type: 'POST', dataType: 'json', data: JSON.stringify(requestObject), contentType: 'application/json; charset=utf-8', success: function (data) { // Clear the current view model VM.Items.length = 0; // only one item coming from server VM.Items[0] = ko.mapping.fromJS(data.BlankItem); // for each property found, bind it to the matching DOM element $.each(VM.Items[0], function (indexInArray, valueOfElement) { var attrName = indexInArray; // skip over things not an accessor (get/set property function) if( typeof valueOfElement == "function") { var attrValue = valueOfElement(); // if it's a checkbox, bind the checked attribute var a = $('input[name="' + attrName + '"][type="checkbox"]'); if (a.length) a.dataBind({ checked: attrName }); // if it's a radio, bind all the found radio checked attributes var b = $('input[name^="' + attrName + '"][type="radio"]'); if (b.length) b.dataBind({ checked: attrName }); // if it's a text, bind the text attribute var c = $('input[name="' + attrName + '"][type="text"]'); if (c.length) c.dataBind({ text: attrName }); // <--- Error (use value) } }); // Then set knockout loose ko.applyBindings( VM.Items[0] ); } }); 

它导致一个错误:

未捕获错误:NO_MODIFICATION_ALLOWED_ERR:DOMexception7
ko.bindingHandlers.updateknockout-1.2.1.debug.js:1577
invokeBindingHandlerknockout-1.2.1.debug.js:1231
ko.applyBindingsToNode.ko.dependentObservable。
disposeWhenNodeIsRemovedknockout-1.2.1.debug.js:1268
evaluateknockout-1.2.1.debug.js:927
ko.dependentObservableknockout-1.2.1.debug.js:965
ko.applyBindingsToNodeknockout-1.2.1.debug.js:1252
ko.applyBindingsknockout-1.2.1.debug.js:1284
ko.utils.arrayForEachknockout-1.2.1.debug.js:48
ko.applyBindingsknockout-1.2.1.debug.js:1283
$ .ajax.successPropertyForm:266
f.extend._Deferred.e.resolveWithjquery-1.6.2.min.js:16
wjquery-1.6.2.min.js:18
f.support.ajax.f.ajaxTransport.send.d

我不认为它绑定任何它不应该的项目。 另外,在html中没有声明性的挖空绑定。 我究竟做错了什么?

我的天啊。 答案是使用正确的绑定属性。 而不是text ,这是一个input的value

在我的情况下,问题是我的数据绑定到文本,而不是价值。

不好:<input type =“text”data-bind =“text:id”maxlength =“3”style =“width:100%;” />

好:<input type =“text”data-bind =“value:id”maxlength =“3”style =“width:100%;” />

我也在Chrome中看到这个错误,当时我不小心多次调用了ko.applyBindings(viewModel)

Interesting Posts