未捕获的TypeError:无法读取属性' nodeType'在ko.cleanNode上未定义

时间:2014-09-26 05:07:54

标签: knockout.js

  var ready;

  ready = function() {
    var imageChooser, productChooser;
    productChooser = function() {
      var self;
      self = this;
      self.quantityId = ko.observable(1);
      self.quantityText = ko.observable('QTY');
      self.genderId = ko.observable(1);
      self.sizeId = ko.observable(1);
      self.colorId = ko.observable(1);
      self.fullSize = ko.observable('SIZE');
      this.setGenderAndSize = function(stringtoparse, thestring) {
        var values;
        values = stringtoparse.split(":");
        self.fullSize("SIZE: " + thestring);
        self.genderId(values[0]);
        self.sizeId(values[1]);
      };
      this.setQuantity = function(quantity) {
        self.quantityId(quantity);
        self.quantityText("QTY: " + quantity);
      };
    };
    imageChooser = function() {
      this.clicked = ko.observable();
      this.setBigImage = (function(message) {
        alert(message);
      }).bind(this);
    };
    ko.applyBindings(new productChooser(), $("#genderAndSizeChooser")[0]);
    ko.cleanNode($('#genderAndSizeChooser')[0]);
    return ko.applyBindings(new imageChooser(), $('#imageChooser')[0]);
  };

  $(document).on('ready page:load', ready);

我目前有这个代码。不知何故,ko.cleanNode代码:

ko.cleanNode($('#genderAndSizeChooser')[0]);

生成错误:

Uncaught TypeError: Cannot read property 'nodeType' of undefined

任何人都知道错误的原因吗?

1 个答案:

答案 0 :(得分:5)

看起来它实际上找不到ID为genderAndSizeChooser的元素。

ko.applyBindings调用会收到undefined并像正常一样对正文应用绑定,因此不会出错,但使用ko.cleanNode调用undefined会导致错误。