Home >Web Front-end >JS Tutorial >javascript select options sorting (maintaining the integrity of the option object)_form effects

javascript select options sorting (maintaining the integrity of the option object)_form effects

WBOY
WBOYOriginal
2016-05-16 18:32:39973browse


[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]<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 name 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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn