Jquery chosen dynamic setting value select Ajax dynamic loading data setting chosen and getting their selected value, jquerychosen
In the edit dialog box, select option must be included. It mainly aims to dynamically load the option and select the option. However, because the chosen () method in jquery is used in the project, the effect cannot be achieved. The source code is as follows:
Java code
- <Select id = "viewOLanguage" data-rel = "chosen">
- <Option value = "zh"> Simplified Chinese (Simplified Chinese) </option>
- <Option value = "en"> English </option>
- <Option value = "fr"> français (French) </option>
- <Option value = "de"> Deutsch (German) </option>
- <Option value = "it"> italiano (Italian) </option>
- <Option value = "es"> espa n ol (Spanish) </option>
- <Option value = "ru"> invalid argument should contain invalid argument (Russian) </option>
- <Option value = "ja"> Japanese (Japanese) </option>
- <Option value = "ko"> Korean </option>
- </Select>
- <Select id = "tLanguage" multiple = "multiple" data-rel = "chosen" style = "width: 80%">
- <Option value = "zh"> Simplified Chinese (Simplified Chinese) </option>
- <Option value = "en"> English </option>
- <Option value = "fr"> français (French) </option>
- <Option value = "de"> Deutsch (German) </option>
- <Option value = "it"> italiano (Italian) </option>
- <Option value = "es"> espa n ol (Spanish) </option>
- <Option value = "ru"> invalid argument should contain invalid argument (Russian) </option>
- <Option value = "ja"> Japanese (Japanese) </option>
- <Option value = "ko"> Korean </option>
- </Select>
The following code is used in the referenced js file:
Js Code
- // Chosen-improves select
- $ ('[Data-rel = "chosen"], [rel = "chosen"]'). chosen ();
Then, it is ineffective to perform any operations on select in your own private js file:
Js Code
- // $ ("# ViewTLanguage option: selected"). attr ("value", language );
- $ ("# ViewTLanguage option [value = '" + language + "']"). attr ("selected", "selected ");
- Jquery. append (option );
So I searched for some solutions on the Internet. I accidentally read several articles and suddenly seemed to understand something.
In fact, you can perform operations before using the jquery chosen () method, and then perform the chosen method.
Js Code
- $ ("# ViewOLanguage option [value = '" + oLanguage + "']"). attr ("selected", "selected ");
- $ ("# ViewOLanguage"). chosen ();
In addition, remove "data-rel =" chosen "" from the select statement, so that the select option can be selected. Similarly, the append method also works.
There is also a saying on the Internet that $ ("# jquery"). chonse ("destroy") is used. I also tried it, but it failed.
You can also try this:
Js Code
- JQuery Chosen. destroy (). init ()
The method of re-instantiation.
Ps: This article is not perfect. The solution mentioned above only solves the initial problem, but does not take into account the subsequent loading. Therefore, you also need to use this code:
Js Code
- $ ("# DialogOLanguage"). trigger ("liszt: updated"); // available in projects
Or in the new version
Js Code
- $ ("# DialogOLanguage"). trigger ("chosen: updated"); // it may be because it can only be used in the new version, and the project has no effect.
This can solve the problem of continuous dynamic loading of the same select statement.
In addition, the destroy method still failed the experiment.