JavaScript Select options sort (maintain option object integrity) _ form Effects

Source: Internet
Author: User

<ptml> <pead></pead> <script> function AddOption (object, Object2) { Each (object2, function (o, index) {object.options[index] = O; })} function Sortlist (SORTNAME,ISDESC) {var what = document.getElementById (sortname); This._options = map (what.options, function (o) {return o; }); This._options.sort (function (A, b) {if (A.text > B.text) {return Isdesc = = True ? 1:-1; else {return Isdesc = = true? -1:1; } }); What.options.length = 0;//Clear Current options addoption (what, this._options); function Map (object, callback, Thisp) {var ret = []; Each.call (Thisp, object, function () {Ret.push (callback.apply thisp, ArgumEnts)); }); return ret; function each (object, callback) {if (undefined = = Object.length) {for (Var na Me in Object {if (false = = Callback (Object[name], name, object) break; } else {for (var i = 0, len = object.length i < len; i++) { if (I in object) {if (false = = Callback (Object[i], I, object)) Break '}} ' var sorder = true; function sort () {if (sorder) {sorder = false; }else{Sorder = true; } sortlist ("Select1", Sorder); } </script> <body> <select id= "Select1" size=6> <option>111</op Tion> <option>333</option> <option>444</option> <option& gt;222</option> <option>333</option> </select> <input Type=butto n value= "Sort" onclick= "sort ();" > </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.