页面上有一个<select id="sel"><option value=1>1111</option>.......</select>
本来是可以用比较传统的办法循环取出来的:for (var i=0;i<sel.length;i++){
alert(sel.options[i].value+","+sel.options[i].text);
}
另外有一种近似的情况:页面上有若干个<input type="text" name="txt">,也是象上面的代码那样,循环取出(当然,没有了.text)。
但我知道javascript里面有个for in的用法。按照例子修改了上面的代码,但总是不能出现上面的效果,都是一些<select>标签或者<input type="text">的一些本身的属性。请问用for in应该怎么写呢?

解决方案 »

  1.   

    for in 是用来得到标签属性的,他不能想上面的for循环一样取值
      

  2.   

    <body>
    <select id="sel"> <option value=1>1111 </option><option value=2>2222 </option> </select> 
    <script>
    var a=document.getElementById('sel').options;
    var b={};
    for(var i=0;i<a.length;i++){
    b[i]=a[i];
    }
    for(i in b)
    {alert(b[i].value)}
    </script>
    </body>
    for in中
    循环的是对象的属性.
      

  3.   


    var o=document.[color=#0000FF]getELementById('sel');
    for (var i=0;i<sel.options.length-1;i++){
        alert(o.options[i].value);
    }[/color]
    var b=document.[color=#FF0000]getElementsByTagName("input");
    for(var j=0;j<b.length-1;j++)
       {
       if(b[j].type=="text")
          alert("第 "+j+" 个类型为text的输入框的值为: "+b[j].value);
       }[/color]
      

  4.   

    var o=document.getELementById('sel'); 
    for (var i=0;i <sel.options.length-1;i++){ 
        alert(o.options[i].value); 
    }
    var b=document.getElementsByTagName("input"); 
    for(var j=0;j <b.length-1;j++) 
      { 
      if(b[j].type=="text") 
          alert("第 "+j+" 个类型为text的输入框的值为: "+b[j].value); 
      } 
      

  5.   

    看来我是不应该这样操作的,应该就用旧方法做就算了。
    2楼的前辈的方法有点搞笑,先用旧方法做一次,然后用for in再做一次。
    算了,不想得太多了。散分。