【问题标题】:Getting an uncaught error "NO_MODIFICATION_ALLOWED_ERR" for input binding获取输入绑定的未捕获错误“NO_MODIFICATION_ALLOWED_ERR”
【发布时间】:2011-10-04 12:23:15
【问题描述】:

我从这个开始:

<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 对象,并将找到的每个属性绑定到 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:DOM 异常 7
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
评估敲除-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 中没有声明性敲除绑定。我做错了什么?

【问题讨论】:

    标签: jquery unobtrusive-javascript knockout.js data-mapping


    【解决方案1】:

    就我而言,问题在于我将数据绑定到文本而不是值。

    不好:

    好:

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        天哪。答案是使用正确的绑定属性。输入不是text,而是value

        【讨论】:

        • 呵呵,看来这很常见。帮帮我,谢谢!
        • 我只是想补充一下,我在 chrome 中看到了这个错误,但在 IE 9 中没有抛出错误。我正在使用 IE9 进行测试/调试,并注意到一切正常,但是当您编辑输入字段中的值时,它并没有更新基础数据。当然,如果绑定到文本而不是值,它如何正确更新。
        • 刚刚也救了我 thnx !
        • 天啊,加我到列表中。谢谢。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-12
        • 1970-01-01
        • 2016-04-28
        • 2020-01-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多