如何动态添加<option>选项

请教各位,我在页面中动态添加新的一行以后,可以在这一行中加入文字,也可以加入文本框和列表框,但是不能将文本框的属性"type"设成"radio",也不能在列表框中加入

 1<option>值.请各位指点,下面是在列中加入文本框的代码.   
 2......   
 3var td_node = document.createElement("td")   
 4inserted_node.appendChild(td_node)   
 5var input_node = document.createElement("input")   
 6td_node.appendChild(input_node)   
 7当我试图将文本框的属性改变时,报错.如下:   
 8input_node.setAttibute("type","radio")   
 9下面这句话也会报错   
10var option_node = document.createElement("option")   
11请指点,多谢!给100分.   
12  
13  
14\---------------------------------------------------------------   
15  
16http://www.csdn.net/Expert/topicview.asp?id=797931   
17\---------------------------------------------------------------   
18  
19var input_node = document.createElement("<input type="radio"/>")   
20  
21\---------------------------------------------------------------   
22  
23IE和NS通用的方法:   
24<form name="f">
25<select name="s">
26<input onclick="a()" type="button" value="add"/>
27</select></form>
28<script>   
29var i=0   
30function a()   
31{   
32b=document.f.s   
33var opt=new Option("text"+ i,"value"+i,true,true)   
34b.options[b.options.length] = opt   
35i++   
36}   
37</script>   
38\---------------------------------------------------------------   
39  
40<select id="demo"></select>
41<script>   
42var option_node = document.createElement("OPTION")   
43option_node.innerText="hehe"   
44demo.appendChild(option_node)   
45</script>   
46\---------------------------------------------------------------   
47  
48<script>   
49var _obj=document.all("test");   
50var _o=document.createElement("Option");   
51_o.text="text";   
52_o.value="1";   
53_obj.add(_o);   
54</script>   
55  
56  
57\---------------------------------------------------------------   
58  
59用javascript实现,the_select为操作的select框,the_array为一个数组,用来赋值,其实不用也可以。   
60  
61function setOptionText(the_select, the_array)   
62{   
63the_select.options.length = the_array.length-1;   
64for (loop=0; loop &lt; the_array.length-1; loop++)   
65{   
66the_select.options[loop].text = the_array[loop];   
67the_select.options[loop].value = the_array[loop];   
68}   
69}</option>
Published At
Categories with Web编程
comments powered by Disqus