<select >
    function setSel(str){
        with(document.all){   
            for(var i=0;i<mysel.options.length;i++){               
                if (mysel.options[i].value==str){
                    mysel.selectedIndex=i;
                    break;
                }
            }           
        }
    }


    function clearSel(){
        with(document.all){   
            mysel.options.length=0;
        }
    }

    function fillSel(){
        with(document.all){
            mysel.options.length=0;
            mysel.options[0] = new Option("1 xxxxxxxxxx","1");
            mysel.options[1] = new Option("2 yyyyyyyyyy","2");
            mysel.options[2] = new Option("3 zzzzzzzzzz","3");
            mysel.options[3] = new Option("4 wwwwwwwwww","4");
            mysel.options[4] = new Option("5 aaaaaaaaaa","5");

            mysel.selectedIndex = 4;
        }
    }

    function removeSel(){
        with(document.all){
            mysel.remove(0);
            if (mysel.options.length>0){
                mysel.selectedIndex=0;
            }
        }
    }

    function editSel(){
        with(document.all){
            if (mysel.options.length>0){
                mysel.options[0] = new Option("这是新的第一项","new Value")
                mysel.selectedIndex=0;
            }
        }
    }

function sortMe(oSel){
var ln = oSel.options.length;
var arr = new Array(); // 这是关键部分

// 将select中的所有option的value值将保存在Array中
for (var i = 0; i < ln; i++)
{
  // 如果需要对option中的文本排序,可以改为arr[i] = oSel.options[i].text;
  arr[i] = oSel.options[i].value;
}

arr.sort(); // 开始排序

// 清空Select中全部Option
while (ln--)
{
  oSel.options[ln] = null;
}

// 将排序后的数组重新添加到Select中
for (i = 0; i < arr.length; i++)
{
  oSel.add(new Option(arr[i], arr[i]));
}
}

</script>

相关文章: