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
260 views
in Technique[技术] by (71.8m points)

javascript - jQuery选择-更新选择列表而不丢失选择(jQuery Chosen - update select list without losing selections)

I'm trying to use jQuery plugin "Chosen"(我正在尝试使用jQuery插件“选择”)

( http://harvesthq.github.com/chosen/ and https://github.com/harvesthq/chosen )(( http://harvesthq.github.com/chosen/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 ))(我想要实现的是基于用户选择的更新列表( ajax调用( 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.(这不是更大的问题,因为我可以使用.chosen().change(function())并删除所有未使用的选择项,然后.append新项。) Then I can use .trigger("liszt:updated") to update list, but unfortunately all selections are deleted..(然后,我可以使用.trigger("liszt:updated")更新列表,但是不幸的是,所有选择都被删除。) 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.(元素,然后用新元素填充,但是获取SELECT“值”数据是一个问题。)   ask by PsychoX translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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

...