<select name='test1'>
<option>1
<option>21
<option>13
<option>ab
<option>33
</select>
<script>
var select=document.f1.test1
var ary=new Array()
for(var i=0;0<select.options.length;i++)
{
ary[i]=select.options[0].text
select.options.remove(0)
}
ary.sort()
for(var i=0;i<ary.length;i++)
{
    var option=document.createElement("Option")
    option.text=ary[i]
    select.add(option)
}
</script>

解决方案 »

  1.   

    楼上的方法可以,如果是生成时可以排序的话,可以在生成select的option值之前进行排序,这样会更好.
      

  2.   

    """如果在資料庫中讀出的就在資料庫中先排序"""那就在資料庫中讀出的SQL語句中就進行排序,這樣省得以後還得在程序中進行處理,
      

  3.   

    有个问题,我的option是带Value的要怎么办?
      

  4.   

    能不能直接对option对像进行排序
      

  5.   

    sql语句把option得value和text都一起查出来,order by value or text随你了
      

  6.   

    MrYou(菜鸟飞飞) 
    你的方法我已经用了,但有在页面有对Select的操作,增删后要做排序,并且不能刷新页面
      

  7.   

    你增删之后调下面的函数就可以了
    <select name='test1'>
    <option value="1">1
    <option value="2">21
    <option value="1">13
    <option value="5">ab
    <option value="3">33
    </select>
    <script>
    function abc()
    {
    var select=document.getElementById('test1')
    var ary=new Array()
    var index=select.options.length
    for(var i=0;i<index;i++)
    {
    ary[i]=new Array(2)
    ary[i][0]=select.options[0].text;
    ary[i][1]=select.options[0].value;
    select.options.remove(0);
    }
    ary.sort()
    for(var i=0;i<ary.length;i++)
    {
        var option=document.createElement("Option")
        option.text=ary[i][0]
        option.value=ary[i][1]
        select.add(option)
    }
    }
    abc()
    </script>
      

  8.   

    我自己也写了一个方法,用了冒泡
    不知道哪个效率高点,呵呵
    function SortItems(source)
    {
    var tempValue = "";
    var tempText = "";
    var tempselect = false;
    for(var i=0;i<source.options.length;i++)
    {
    for(var j=0;j<source.options.length-i-1;j++)
    {
    if(source.options[j].text > source.options[j+1].text)
    {
    tempValue = source.options[j].value;
    tempText = source.options[j].text;
    tempselect = source.options[j].selected;
    source.options[j].value = source.options[j+1].value;
    source.options[j].text = source.options[j+1].text;
    source.options[j].selected = source.options[j+1].selected;
    source.options[j+1].value = tempValue;
    source.options[j+1].text = tempText;
    source.options[j+1].selected = tempselect
    }
    }
    }
    }
      

  9.   

    冒泡绝对效率低,系统的sort应该快!