2012-06-10 63 views
0

我在我的網站中使用了Chosen Select Box Jquery插件。選擇中的選項會使用ajax進行更新,因此當您在1個框中選擇某個內容時,下一個框的選項會顯示出來。FireBug - TypeError:項目未定義

我已經差不多工作了,但是當我點擊第二個框選項第三個框選項不顯示。我得到在Firebug的錯誤說:

類型錯誤:項目是未定義

item.selected = TRUE;

它說它是在選擇框的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(); 
    } 
}; 

我花了幾個小時試圖數字出來,但不能。任何幫助,將不勝感激。

+0

你確定'results_data'不是空的嗎?如果是,它是否在'high_id.substr(high_id.lastIndexOf(「_」)+ 1)'的結果中有一個值? – Oded

+0

@BenjaminGruenbaum我只是想嘗試一些事情。 –

+0

@Oded我希望這聽起來不像一個愚蠢的問題,但我如何檢查它是否爲空。因爲我認爲它是在頁面加載時,但不晚於 –

回答

6

如果遇到同樣的問題,請在您選擇的HTML中添加一個空的選項標籤,並應該正常工作。

編輯:由knuturO指出,數組是0索引,但大多數選擇的索引從1開始。通過添加一個空值,不再存在數組索引和所選索引之間的不匹配。例如,下拉菜單的工作方式相同。

+0

不幸的是我不再在這個項目上工作。這是令人失望的,因爲我從來沒有得到它的工作,最終無法使用Chosen。很高興你能夠修復它。 –

+0

我不知道我的解決方案,解決了你的確切問題,但它從來沒有痛過嘗試,也許你會得到它:) – user3198079

+0

這種解決方案爲我工作。這是由於一個問題,因爲數組從0開始,選擇索引從1開始。通過添加value =''和text =''的項目,它將不會顯示,但將存在於陣列中,修復關閉一個錯誤。 – KnuturO

相關問題