0 0

javascript 的问题,在ie6中不能正常得到option 的值,FF中无问题,请大家帮帮我20

jsp页面上有2个select输入框,分别是:
<select onChange="onMethodSelected()" id="methodselect" >
    <option value="1">FILE</option>
    <option value="0">DATABASE</option>
</select>


<select id="tableNames" >
</select>

有两个选项,现在需要,如果第一个选中DATABASE 第二个选择框就应该读出数据库的表名。
结果是,在IE中显示都一切正常,也能读出,也能选择表名,但是就是document.getElementById("tableNames").value 是空值。

ajax的部分代码:
通过,appendChild加进第二个select中去的
var tableList = document.getElementById("tableNames");
var tableNames = xmlHttp.responseXML.getElementsByTagName("tableName");
var option = null;
for (var i = 0; i < tableNames.length; i++) {
	option = document.createElement("option");
	option.appendChild(document.createTextNode(tableNames[i].firstChild.nodeValue));
	tableList.appendChild(option);
}


这样在第一个select onChange调用的onMethodSelected()中document.getElementById("tableNames").value得不到第二个select中选择的值,这是为什么呢?
alert(document.getElementById("tableNames"))是[object]
alert(document.getElementById("tableNames").value)啥都没有

在FireFox中一切正常,IE6中就不行,看了网上说的IE中id 和name的bug,但是我这里变貌似也没有冲突的id 和name 啊,,,郁闷中,求大家帮忙
问题补充:
但是写一个测试文件就可以得到比如
<html>
	<body>
		<script type="text/javascript">
			
			function sayOption(){
			 	var word=document.getElementById("aaa").value;
				alert(word)
			}
		</script>
	<select id="aaa">
		<option value="1">1</option>
		<option value="2">2</option>
	</select>
	<input type="button" onClick="sayOption()"/>
	</body>
</html>


是不是appendchild 的原因呢,如果用appendchild 怎么能得到option的值
问题补充:
我找到原因了,如果有朋友也遇到我的问题可以参考我的这个,问题就出在
for (var i = 0; i < tableNames.length; i++) {  
    option = document.createElement("option");  
      option.appendChild(document.createTextNode(tableNames[i].firstChild.nodeValue)); 
tableList.appendChild(option);  



这个只是append了一个child 这样就相当于只加入了option的显示,如果对应html的源吗应该就是:
<select ....>
    <option>读出的表名1</option>
    <option>读出的表名2</option>
</select>

这样是不行的,因为没有并没有value属性,
所以要在前面的代码中加入

option.setAttribute("value",tableNames[i].firstChild.nodeValue);

如下:
for (var i = 0; i < tableNames.length; i++) {  
    option = document.createElement("option");  
      option.appendChild(document.createTextNode(tableNames[i].firstChild.nodeValue));
option.setAttribute("value",tableNames[i].firstChild.nodeValue);
tableList.appendChild(option);  


这样就可以通过value取到值啦!

这也说明了firefox比IE聪明很多了。。。。我想firefox中应该就是看没有value属性的话他就默认吧显示的文本当作选项的value吧


问题补充:
谢谢关注的朋友,谢谢!
2009年8月31日 14:09

2个答案 按时间排序 按投票排序

0 0


这样应该能通过
var  xmlDoc=req.responseXML.documentElement;
    var  xSel=xmlDoc.getElementsByTagName('select');
    var  select_root=document.getElementById('list1');
    select_root.options.length=0;
    if(xSel.length!=0)
    {
    for(var  i=0;i<xSel.length;i++)
    {
         var  xValue=xSel[i].childNodes[0].firstChild.nodeValue;
         var  xText=xSel[i].childNodes[1].firstChild.nodeValue;
         try{
         select_root.options[i]=new  Option(xText,xValue);
         }catch(e){
         }
    }
    }

2009年8月31日 14:56
0 0

ie下取select的选中值不是这么取的


var tableList = document.getElementById("tableNames");  

这样得到选中的值tableList.options[tableList .options.selectedIndex].value

2009年8月31日 14:17

相关推荐

Global site tag (gtag.js) - Google Analytics