求一个javascript的select中onchange事件的解决方法!
<HTML>
<head>
<SCRIPT language="javascript">
function showpublishtel()
{
if(document.getElementById("age").value=="18")
{
name.innerText="DEF:"
}
if(16==document.getElementById("age").value=="16")
{
name.innerText="ABC:"
}
}
</SCRIPT>
</HEAD>
<BODY>
<form name="form" action="" method="post">
<table>
<div id="name">年龄:</div>
<td>
<SELECT id="age" name="age" onchange="showpublishtel();">
<OPTION value=16>16岁 </OPTION>
<OPTION value=18>18岁</OPTION>
<OPTION value=20>20岁</OPTION>
</select>
</td>
</table>
</form>
</BODY>
</HTML>
就是当我选择16岁时候,id是name插入文本"ABC"
当我选择18岁时候 id是name插入文本"DEF"
大家看下我哪写错了,现在什么都出不来!!
------解决方案--------------------showpublishtel(this.options[this.selectedIndex].value);
把选定的值传递进去作判断.
------解决方案--------------------name.innerText="DEF:"
改为
document.getElementById("name").innerText="DEF:"
------解决方案--------------------如果是我,我会这样写.
<HTML >
<head >
<SCRIPT language="javascript" >
function showpublishtel(obj) {
var text = document.getElementById("name");
if(obj.value=="18")
text.innerText="DEF:";
if(obj.value =="16")
text.innerText="ABC:";
}
</SCRIPT >
</HEAD >
<BODY >
<form name="form" action="" method="post" >
<table >
<div id="name" >年龄: </div >
<td >
<SELECT id="age" name="age" onchange="showpublishtel(this)" >
<OPTION value=16 >16岁</OPTION >
<OPTION value=18 >18岁</OPTION >
<OPTION value=20 >20岁</OPTION >
</select >
</td >
</table >
</form >
</BODY >
</HTML >
------解决方案--------------------<HTML >
<head >
<SCRIPT language="javascript" >
function showpublishtel()
{
var name = document.getElementById("name");
if(document.getElementById("age").value=="18")
{
name.innerText="DEF:" ;
}
if(document.getElementById("age").value=="16")
{
name.innerText="ABC:" ;
}
}
</SCRIPT >
</HEAD >
<BODY >
<form name="form" action="" method="post" >
<table >
<div id="name" >年龄: </div >
<td >
<SELECT id="age" name="age" onchange="showpublishtel()" >
<OPTION value=16 >16岁 </OPTION >
<OPTION value=18 >18岁 </OPTION >
<OPTION value=20 >20岁 </OPTION >
</select >