<script>
window.onload=function(){
var sel = document.forms[0].rk_sslb;
for(i=0;i<sel.options.length;i++){
for(j=i+1;j<sel.options.length;j++){
if(sel.options[i].value==sel.options[j].value) sel.options.remove(j);
}
}
}
</script>
<form>
<select name="rk_sslb" id="rk_sslb">
<option value="1">1 </option>
<option value="1">1 </option>
<option value="2">2 </option>
<option value="3">3 </option>
<option value="3">3 </option>
</select>
</form>
//方法二
<script>
var s = document.getelementbyid('rk_sslb').options, c = s.length;
while(c--)
{
for(var i=0; i<c; i++)
{
if(s[i].value == s[c].value)
{
s[c] = null;
break;
}
}
}
</script>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>删除select中重复项</title>
<style type="text/css教程">
body{ text-align:center;}
div{ width:400px; background:#f1f5fa; margin:auto; border:solid 1px #bfc9db; padding:10px;}
h4{ }
a{text-align:right; display:block; font-size:12px;}
</style>
<script type="text/网页特效">
/*定义全局函数$*/
function $(id){
return document.getelementbyid(id);
}
/*初始化select*/
function initialselectoption(id){
var osel=$(id);
var aoptions=["wang hongjian","wang hongjian","nichoal smith","nichoal smith","david gates","david gates","david gates","wang hongjian","wang hongjian","nichoal smith","nichoal smith","david gates","david gates","david gates"];
var i=0;
osel.options.length=0;
while(i<aoptions.length){
var option=new option(aoptions[i],i);
osel.options.add(option);
i++;
}
osel.setattribute("size",i-1);
$("btndistinct").removeattribute("disabled");
}
/*删除重复项*/
function distinctselectoption(id){
var osel=$(id);
var i=0;
while(i<osel.options.length){
var j=i+1;
while(j<osel.options.length){
if(osel.options[i].text==osel.options[j].text){
osel.options[j]=null;//不可使用osel.options.remove(j),因为不兼容firefox
}else{
j++;
}
}
i++;
}
osel.setattribute("size",i);
$("btndistinct").setattribute("disabled","disabled");
}
window.onload=function(){
/*初始化*/
$("btninital").onclick=function(){initialselectoption("sel");};
/*删除重复项*/
$("btndistinct").onclick=function(){distinctselectoption("sel");};
$("btndistinct").setattribute("disabled","disabled");
}
</script>
</head><body>
<div>
<h4>删除select重复项演示</h4>
<a href="http://www.111cn.net/" target="_blank" title="http://www.111cn.net/">http://www.111cn.net/</a>
<hr />
<select id="sel" multiple="multiple" size="1"><option>待初始化…</select>
<hr />
<button id="btninital">初始化</button>
<button id="btndistinct">删除重复项</button>
</div>
</body>
</html>