Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
723 views
in Technique[技术] by (71.8m points)

javascript - jQuery Chosen - update select list without losing selections

I'm trying to use jQuery plugin "Chosen"

(http://harvesthq.github.com/chosen/ and https://github.com/harvesthq/chosen)

in my project.

What I'm trying to achieve is update list basing on user selection (ajax call (tree based structure))

This is no bigger problem, because i can use .chosen().change(function()) and remove all unused select items and then .append new ones.

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

elements and then populate with new ones, but then is a problem with getting SELECT "value" data. See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...