2012-07-13 66 views
1

我想取消選擇的允許多重選擇使用此代碼chosen plugin選擇選擇插件

var lstViewCities= $('#lstViewCities'); 
$(lstViewCities).find("option").each(function(item) { 
     $(item).attr('selected', ''); 
}); 
$(lstViewCities).trigger("liszt:updated"); 

選擇所有值,但什麼也沒發生,並同樣選擇所有選項

var lstViewCities= $('#lstViewCities'); 
$(lstViewCities).find("option").each(function(item) { 
     $(item).attr('selected', 'selected'); 
}); 
$(lstViewCities).trigger("liszt:updated"); 
所有選項

[編輯]
這裏是live example對於上述問題
[/編輯]

它不支持使用代碼或什麼選擇多個選項嗎? 您的答案肯定會有很大的幫助,期待您的回覆。謝謝:)

+0

Zaksh你可以添加的jsfiddle昨天晚上?我會幫助回答這個問題。 – Undefined 2012-07-13 06:26:26

+0

@Sam這裏是鏈接http://jsfiddle.net/3qrEQ/8/ – Zaksh 2012-07-13 06:33:16

回答

1
$("#btn").click(function(){ 
    $('.search-choice-close', '.search-choice').trigger('click'); 
} 

在單選擇您可以使用:

$("#btn").click(function(){ 
    $('#testChosen_chzn').find('.search-choice-close').trigger('click'); 
} 
+0

不幸的是它不工作:( – Zaksh 2012-07-13 06:39:58

+0

編輯工作正常:) – 2012-07-13 06:47:55

+0

老兄我真的很感謝你的努力,我工作的魅力,但如果我有多個選擇框,我只想休息一個,絕對不能使用基於類名稱的通配符條目 – Zaksh 2012-07-13 06:54:29

0

試試這個,

$(lstViewCities).find("option").each(function(this) { 
    $(this).attr('selected', 'selected'); 
}); 

取代 '這個' 而不是 '項目'

3

如這裏說明:https://github.com/harvesthq/chosen/issues/1559

你可以這樣做:

// Deselect 
$('#my_select_box option:selected').removeAttr('selected'); 
$('#my_select_box').trigger('chosen:updated'); 

// Select 
$('#my_select_box option').attr('selected', 'selected'); 
$('#my_select_box').trigger('chosen:updated'); 

我只是用它和它的工作,問題是,如果事件被在接下來的版本更新 事情只會停止工作