問題描述
我正在嘗試使用 jQuery 插件Chosen"
I'm trying to use jQuery plugin "Chosen"
(http://harvesthq.github.com/chosen/ 和 https://github.com/harvesthq/chosen)
在我的項目中.
我想要實現的是基于用戶選擇的更新列表(ajax
調用(tree based structure
))
What I'm trying to achieve is update list basing on user selection (ajax
call (tree based structure
))
這不是更大的問題,因為我可以使用 .chosen().change(function())
并刪除所有未使用的選擇項,然后 .append 新的.
This is no bigger problem, because i can use .chosen().change(function())
and remove all unused select items and then .append new ones.
然后我可以使用 .trigger("liszt:updated")
更新列表,但不幸的是所有選擇都被刪除了..
Then I can use .trigger("liszt:updated")
to update list, but unfortunately all selections are deleted..
有誰知道如何在不丟失所選數據的情況下更新所選列表?
Does anyone know a way how to update chosen list without loosing selected data?
理論上,我可以手動刪除所有選擇的生成
In theory I can manually remove all chosen generated
推薦答案
如果您保存選定的項目,這應該相當簡單.例如:
This should be fairly simply if you save the items selected. For example:
<select data-placeholder="Choose a country..." style="width:350px;" multiple="true" class="chosen-select">
$(".chosen-select").chosen();
現在,在更新所選項目之前,請確保您保存所選項目,如下所示:
Now, before updating the chosen, make sure you save the items selected like this:
var chosenSelectedItems = $(".chosen-select").val(); // this gets you the select value data
// Update the select items
$('.chosen-select').trigger('liszt:updated');
$(".chosen-select").val(chosenSelectedItems);
這應該可以在更改之前重置原始值.
This should be able to reset the original values before the change.
這篇關于jQuery Chosen - 更新選擇列表而不丟失選擇的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!