日期:2014-05-16  浏览次数:20417 次

把json数据赋值给下拉框,下拉框值不能重复

JScript code
<body onload="fun()">
<script>
function fun()
    var sel=document.getElementById("sel");
    var json={"list":[{"type":"1","name":"电信"},{"type":"1","name":"电信"},{"type":"2","name":"联通"}]};
    sel.add(new Option("请选择",""));
    for(var i=0;i<json.list.length;i++){
        sel.add(new Option(json.list[i].name,json.list[i].type)); 
    }
}
</script>
<select id="sel" name="sel"></select>
</body>


------解决方案--------------------
fun() 后面少了一个大括号:

<body onload="fun()">
<script>
function fun() {
var sel=document.getElementById("sel");
var json={"list":[{"type":"1","name":"电信"},{"type":"1","name":"电信"},{"type":"2","name":"联通"}]};
sel.add(new Option("请选择",""));
for(var i=0;i<json.list.length;i++){
sel.add(new Option(json.list[i].name,json.list[i].type)); 
}
}
</script>
<select id="sel" name="sel"></select>
</body>
------解决方案--------------------
探讨
fun() 后面少了一个大括号:

<body onload="fun()">
<script>
function fun() {
var sel=document.getElementById("sel");
var json={"list":[{"type":"1","name":"电信"},{"type":"1","name":"电信"},{"type":"2","nam……

------解决方案--------------------
HTML code

function fun(){
    var sel=document.getElementById("sel");
    var json={"list":[{"type":"1","name":"电信"},{"type":"1","name":"电信"},{"type":"2","name":"联通"}]};
    sel.add(new Option("请选择",""));
    var theforever_csdn='|';
    for(var i=0;i<json.list.length;i++){
        if(theforever_csdn.indexOf('|'+json.list[i].name+'|')){
            theforever_csdn+=json.list[i].name+'|';
            sel.add(new Option(json.list[i].name,json.list[i].type)); 
        }
    }
}