FireBug - TypeError:item未定义

时间:2012-06-10 19:07:39

标签: jquery firebug drop-down-menu jquery-chosen

我正在使用我网站上的Chosen Select Box Jquery插件。 select中的选项使用ajax更新,因此当您在1框中选择某些内容时,将显示下一个框的选项。

我几乎让它工作但是当我点击第二个框选项时,第三个框选项不显示。我在FireBug中收到错误说:

TypeError:item is undefined

item.selected = true;

它说它在Chosen Box的js文件中。

JS =======================

 Chosen.prototype.result_select = function(evt) {
  var high, high_id, item, position;
  if (this.result_highlight) {
    high = this.result_highlight;
    high_id = high.attr("id");
    this.result_clear_highlight();
    if (this.is_multiple) {
      this.result_deactivate(high);
    } else {
      this.search_results.find(".result-selected").removeClass("result-selected");
      this.result_single_selected = high;
      this.selected_item.removeClass("chzn-default");
    }
    high.addClass("result-selected");
    position = high_id.substr(high_id.lastIndexOf("_") + 1);
    item = this.results_data[position];
    item.selected = true;                                <---------- Error
    this.form_field.options[item.options_index].selected = true;
    if (this.is_multiple) {
      this.choice_build(item);
    } else {
      this.selected_item.find("span").first().text(item.text);
      if (this.allow_single_deselect) this.single_deselect_control_build();
    }
    if (!(evt.metaKey && this.is_multiple)) this.results_hide();
    this.search_field.val("");
    if (this.is_multiple || this.form_field_jq.val() !== this.current_value) {
      this.form_field_jq.trigger("change", {
        'selected': this.form_field.options[item.options_index].value
      });
    }
    this.current_value = this.form_field_jq.val();
    return this.search_field_scale();
  }
};

我花了好几个小时试图搞清楚但不能。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:6)

如果出现同样的问题,请在您的选择HTML中添加一个空的选项标记,应该可以正常工作。

编辑:正如knuturO指出的那样,数组被索引为0,但大多数选择的索引从1开始。通过添加空值,数组索引与所选索引之间不再存在不匹配。例如,下拉列表的工作方式相同。