Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在绑定的内部使用foreach时传递值的knockoutjs抛出引用错误_Javascript_Data Binding_Knockout.js_Referenceerror - Fatal编程技术网

Javascript 在绑定的内部使用foreach时传递值的knockoutjs抛出引用错误

Javascript 在绑定的内部使用foreach时传递值的knockoutjs抛出引用错误,javascript,data-binding,knockout.js,referenceerror,Javascript,Data Binding,Knockout.js,Referenceerror,我正在尝试使用knockoutjs动态创建select输入 我的看法是这样的- <div data-bind="with: QuestionFilter"> <form> <div> <select data-bind="options: categories, optionsText: 'name', optionsValue: 'categoryID', value: selectedCategory"> &

我正在尝试使用knockoutjs动态创建select输入

我的看法是这样的-

<div data-bind="with: QuestionFilter">
  <form>
    <div>
      <select data-bind="options: categories, optionsText: 'name', optionsValue: 'categoryID', value: selectedCategory">
      </select>
    </div>
    <div data-bind="foreach: details">
      <select data-bind="options: subcategories, optionsText: 'name', optionsValue: 'categoryID', value: selectedSubcategory">
      </select>
    </div>
  </form>
</div>  
<script type="text/javascript">
  ko.applyBindings({
    categories =  <?php echo json_encode($categories); ?>,        
    details = ko.observableArray([])       
  });
</script>
function QuestionFilter(categories, details) {
  var self = this;
  self.categories = ko.observableArray(categories);
  self.subcategories = ko.observableArray([]);
  self.selectedCategory = ko.observable();
  self.selectedSubcategory = ko.observable();
  self.superSubcategories = ko.observableArray([]);

  self.selectedCategory.subscribe(function(category) {
    function search(nameKey, myArray){
      for (var i=0; i < myArray.length; i++) {
        if (myArray[i].parentCategory_id === nameKey) {
          self.subcategories.push(myArray[i]);
        }
      }
    }   
    search(category, categories);
    details.push({ firstName: self.subcategories()});
  });

  self.selectedSubcategory.subscribe(function(subcategory) {
    function subsearch(nameKey, myArray){
      for (var i=0; i < myArray.length; i++) {
        if (myArray[i].parentCategory_id === nameKey) {
          self.superSubcategories.push(myArray[i]);
    }
      }
    }
   subsearch(subcategory, categories);
   details.push({ firstName: self.superSubcategories()});
  });
我的JS看起来像这样-

<div data-bind="with: QuestionFilter">
  <form>
    <div>
      <select data-bind="options: categories, optionsText: 'name', optionsValue: 'categoryID', value: selectedCategory">
      </select>
    </div>
    <div data-bind="foreach: details">
      <select data-bind="options: subcategories, optionsText: 'name', optionsValue: 'categoryID', value: selectedSubcategory">
      </select>
    </div>
  </form>
</div>  
<script type="text/javascript">
  ko.applyBindings({
    categories =  <?php echo json_encode($categories); ?>,        
    details = ko.observableArray([])       
  });
</script>
function QuestionFilter(categories, details) {
  var self = this;
  self.categories = ko.observableArray(categories);
  self.subcategories = ko.observableArray([]);
  self.selectedCategory = ko.observable();
  self.selectedSubcategory = ko.observable();
  self.superSubcategories = ko.observableArray([]);

  self.selectedCategory.subscribe(function(category) {
    function search(nameKey, myArray){
      for (var i=0; i < myArray.length; i++) {
        if (myArray[i].parentCategory_id === nameKey) {
          self.subcategories.push(myArray[i]);
        }
      }
    }   
    search(category, categories);
    details.push({ firstName: self.subcategories()});
  });

  self.selectedSubcategory.subscribe(function(subcategory) {
    function subsearch(nameKey, myArray){
      for (var i=0; i < myArray.length; i++) {
        if (myArray[i].parentCategory_id === nameKey) {
          self.superSubcategories.push(myArray[i]);
    }
      }
    }
   subsearch(subcategory, categories);
   details.push({ firstName: self.superSubcategories()});
  });
因此,变量selectedCategory将按预期执行,并触发self.selectedCategory.subscribe中的函数。变量selectedSubcategory将不会按预期执行,而是引发以下错误-ReferenceError:selectedSubcategory未定义。我认为这与在with绑定中使用foreach绑定有关,但我不确定。其他一切都按预期工作,我甚至使用它成功地向details数组添加了几个对象


需要任何想法或澄清吗?

看起来我没有在正确的上下文中访问selectedSubcategory。正在为$parents[0]预加载。给出了期望的结果。因此它看起来像$parents[0]。selectedSubcategory。奇怪的是,$parents应该提供与$parents[0]相同的上下文,但在这种情况下它不起作用

你的ko.applyBindings调用看起来不像是有效的语法。嗨,伊斯梅尔,我不得不把东西缩减一点,这样它就很适合这里了。我可以向您保证它是有效的,因为上面代码中的所有内容都有效,除了使用selectedSubcategory.subscribe位。