Jquery chosen dynamic setting value select Ajax dynamic loading data setting chosen and getting their selected value, jquerychosen

Source: Internet
Author: User

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
  1. <Select id = "viewOLanguage" data-rel = "chosen">
  2. <Option value = "zh"> Simplified Chinese (Simplified Chinese) </option>
  3. <Option value = "en"> English </option>
  4. <Option value = "fr"> français (French) </option>
  5. <Option value = "de"> Deutsch (German) </option>
  6. <Option value = "it"> italiano (Italian) </option>
  7. <Option value = "es"> espa n ol (Spanish) </option>
  8. <Option value = "ru"> invalid argument should contain invalid argument (Russian) </option>
  9. <Option value = "ja"> Japanese (Japanese) </option>
  10. <Option value = "ko"> Korean </option>
  11. </Select>
  12. <Select id = "tLanguage" multiple = "multiple" data-rel = "chosen" style = "width: 80%">
  13. <Option value = "zh"> Simplified Chinese (Simplified Chinese) </option>
  14. <Option value = "en"> English </option>
  15. <Option value = "fr"> français (French) </option>
  16. <Option value = "de"> Deutsch (German) </option>
  17. <Option value = "it"> italiano (Italian) </option>
  18. <Option value = "es"> espa n ol (Spanish) </option>
  19. <Option value = "ru"> invalid argument should contain invalid argument (Russian) </option>
  20. <Option value = "ja"> Japanese (Japanese) </option>
  21. <Option value = "ko"> Korean </option>
  22. </Select>

 

The following code is used in the referenced js file:

 

Js Code
  1. // Chosen-improves select
  2. $ ('[Data-rel = "chosen"], [rel = "chosen"]'). chosen ();

 

Then, it is ineffective to perform any operations on select in your own private js file:

 

Js Code
  1. // $ ("# ViewTLanguage option: selected"). attr ("value", language );
  2. $ ("# ViewTLanguage option [value = '" + language + "']"). attr ("selected", "selected ");
  3. 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
  1. $ ("# ViewOLanguage option [value = '" + oLanguage + "']"). attr ("selected", "selected ");
  2. $ ("# 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
  1. 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
  1. $ ("# DialogOLanguage"). trigger ("liszt: updated"); // available in projects

Or in the new version

 

Js Code
  1. $ ("# 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.

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.