JavaScript select list content sorted alphabetically and order BY list _ form effects

Source: Internet
Author: User
Today, 51JS has posted this problem, the original thought is to the content alphabetically order, because the previous read a positive order of the article, I would like to reverse should not be difficult, looked at the data will quickly fix the problem.
<textarea id="runcode81434"><ptml> <pead> <title> Worry-free script code test page </title> </pead> <body> <select name= "a" I D= "A" > <option value= "b" >b</OPTION> <option value= "E" >e</OPTION> <option value= "C" >c</OPTION> <option value= "F" >f</OPTION> <option value= "a" >a</OPTION> </ select> <script type= "Text/javascript" > Function st (a,b) {if (a>b) return-1; else if (a<b) return 1; else return 0; function sort (id) {var s=document.getelementbyid (ID); if (!s) return; var t1=[],t2=[],tt=s.options; while (tt.length>0) {t1[t1.length]=tt[0].text; T2[t2.length]=tt[0].value; Tt.remove (0); } t1.sort (ST); for (Var i=0,c1,c2;c1=t1[i],c2=t2[i];i++) {Tt.add (new Option (C1,C2)); } sort ("a"); </script> </body> </ptml></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

But look back after the original landlord no problem description Clearly, his intention is to list content in reverse order, that is, ABCEF into FECBA.
Beginning to think that the difficult solution, suddenly recalled that the array has a reverse () function (the array content in reverse order), the problem suddenly become very simple.
<textarea id="runcode73148"><ptml> <pead> <title> Worry-free script code test page </title> </pead> <body> <select name= "a" I D= "A" > <option value= "b" >b</OPTION> <option value= "E" >e</OPTION> <option value= "C" >c</OPTION> <option value= "F" >f</OPTION> <option value= "a" >a</OPTION> </ select> <script type= "Text/javascript" > function sort (id) {var s=document.getelementbyid (ID); if (!s) return; var t1=[],t2=[],tt=s.options; while (tt.length>0) {t1[t1.length]=tt[0].text; T2[t2.length]=tt[0].value; Tt.remove (0); } t1.reverse (); T2.reverse (); for (Var i=0,c1,c2;c1=t1[i],c2=t2[i];i++) {Tt.add (new Option (C1,C2)); } sort ("a"); </script> </body> </ptml></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Related Article

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.