2013-10-31 42 views
1

我正在使用選定的插件來構建多個選擇輸入字段。在此處看到一個示例:http://harvesthq.github.io/chosen/#multiple-selectJquery Chosen插件。選擇多個相同的選項

默認行爲禁用選項,如果它已被選中。在上面的例子中,如果你選擇「阿富汗」,它會在下拉菜單中變灰,從而不允許你再次選擇它。

我需要能夠多次選擇相同的選項。插件或手動控制中是否有任何設置可以添加,這將允許這樣做?

回答

0

對於一種變通方法,可以使用下面的代碼上的每個選擇(在選擇事件)或同時彈出打開:

$(".chosen-results .result-selected").addClass("active-result").removeClass("result-selected"); 

上述代碼除去結果選擇類和添加的有源結果班上的李項目。因此,每個選定的項目都被視爲活動結果,現在您可以再次選擇該項目。

+0

我嘗試添加該代碼我的選擇事件:'$( 「chzn選」)選擇(函數( ){\\ code here});'這是行不通的,因爲選擇的jQuery會用div和whatnot覆蓋select,從而導致select事件不會觸發。難道我做錯了什麼? – Jeremy

+0

實際上,在選擇時,當您選擇一個值時,添加了「結果選擇」類來貼心地選擇該值。以便我們覆蓋該類並將其替換爲默認類。在選擇事件中,放置一個調試器並確認選擇事件已完成。否則請在** popup open **事件中嘗試此代碼。 – Soundar

+0

實際的概念是,當你運行代碼的時候應該完成選擇。 (即)「結果選擇」類應添加到相應的項目中。 – Soundar

1

我創建了一個選擇版本,允許您多次選擇相同的項目,甚至可以將這些多個項目作爲POST變量發送到服務器。這裏是你如何能做到這一點(很容易,我認爲):

(提示:使用chosen.jquery.js一個搜索功能來查找這些行)


變化:

this.is_multiple = this.form_field.multiple; 

要:

this.is_multiple = this.form_field.multiple; 
this.allows_duplicates = this.options.allow_duplicates; 

變化:

classes.push("result-selected"); 

要:

if (this.allows_duplicates) { 
    classes.push("active-result"); 
} else { 
    classes.push("result-selected"); 
} 

變化:

this.form_field.options[item.options_index].selected = true; 

要:

if (this.allows_duplicates && this.form_field.options[item.options_index].selected == true) { 
    $('<input>').attr({type:'hidden',name:this.form_field.name,value:this.form_field.options[item.options_index].value}).appendTo($(this.form_field).parent()); 
} else { 
    this.form_field.options[item.options_index].selected = true; 
} 

然後調用chosen()時,一定要包括allows_duplicates選項:

$("mySelect").chosen({allow_duplicates: true}) 
相關問題