javascript高手帮忙看一个下拉列表的问题

cpzhg 2006-01-20 10:28:35
<select name="text" onChange="test_change()">
<option value="" selected>请选择</option>
<option value="1" selected>test1</option>
<option value="2" selected>test2</option>
<option value="3" selected>test3</option>
</select>

<tr>
<td>test1</td>
</tr>

<tr>
<td>test2</td>
</tr>

如果我在下拉列表框中选择"test1"就出现第一个tr,同理我选择"test2"就出现第二个tr,网页在加载时什么都不显示,请问这个test_change()怎么写呀,请高手帮忙


...全文
233 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
meizz 2006-01-20
  • 打赏
  • 举报
回复
<select name="text" onChange="test_change(this)">
<option value="" selected>请选择</option>
<option value="1" selected>test1</option>
<option value="2" selected>test2</option>
<option value="3" selected>test3</option>
</select>

<table id="tab" border=1>
<tr style="display: none">
<td>test1</td>
</tr>

<tr style="display: none">
<td>test2</td>
</tr>

<tr style="display: none">
<td>test2</td>
</tr>

</table>

<SCRIPT LANGUAGE="JavaScript">
function test_change(e)
{
var tab = document.getElementById("tab");
for(var i=0; i<tab.rows.length; i++)
tab.rows[i].style.display = "none";
var s = e.value;
if(s!="")
{
tab.rows[parseInt(s, 10) - 1].style.display = "";
}
}
</SCRIPT>
cpzhg 2006-01-20
  • 打赏
  • 举报
回复
谢谢meizz(梅花雪)大哥,可以了!
meizz 2006-01-20
  • 打赏
  • 举报
回复
<select name="text" onChange="test_change(this)">
<option value="" selected>please select</option>
<option value="test1" selected>test1</option>
<option value="test2" selected>test2</option>
<option value="test3" selected>test3</option>
</select>

<table id="tab" border=1>
<tr style="display: none">
<td>test1</td>
</tr>

<tr style="display: none">
<td>test2</td>
</tr>

<tr style="display: none">
<td>test3</td>
</tr>

</table>

<SCRIPT LANGUAGE="JavaScript">
function test_change(e)
{
var tab = document.getElementById("tab");
for(var i=0; i<tab.rows.length; i++)
tab.rows[i].style.display = "none";
var s = e.selectedIndex;
if(s>0)
{
tab.rows[s - 1].style.display = "";
}
}
</SCRIPT>
ice_berg16 2006-01-20
  • 打赏
  • 举报
回复
prototype.js 稍微看了一下,不妥之处与错误不少

我看了很长时间,有些代码还没有转过弯来,你觉得哪些地方有问题?
cpzhg 2006-01-20
  • 打赏
  • 举报
回复
我测试过了,不行呀!
meizz 2006-01-20
  • 打赏
  • 举报
回复
你试试看我改过的JS代码呀!自己连测试都不测试一下
cpzhg 2006-01-20
  • 打赏
  • 举报
回复
<select name="text" onChange="test_change(this)">
<option value="" selected>请选择</option>
<option value="test1" selected>test1</option>
<option value="test2" selected>test2</option>
<option value="test3" selected>test3</option>
</select>

你没有理解我的意思,如果value的值为test1,test2,test3时js该怎么写?
meizz 2006-01-20
  • 打赏
  • 举报
回复
<select name="text" onChange="test_change(this)">
<option value="" selected>请选择</option>
<option value="1" selected>test1</option>
<option value="2" selected>test2</option>
<option value="3" selected>test3</option>
</select>

<table id="tab" border=1>
<tr style="display: none">
<td>test1</td>
</tr>

<tr style="display: none">
<td>test2</td>
</tr>

<tr style="display: none">
<td>test2</td>
</tr>

</table>

<SCRIPT LANGUAGE="JavaScript">
function test_change(e)
{
var tab = document.getElementById("tab");
for(var i=0; i<tab.rows.length; i++)
tab.rows[i].style.display = "none";
var s = e.selectedIndex;
if(s!=-1)
{
tab.rows[s - 1].style.display = "";
}
}
</SCRIPT>
cpzhg 2006-01-20
  • 打赏
  • 举报
回复
谢谢meizz(梅花雪)大哥,但现在有一个新问题,如果value值不是1,2,3而是相对应的test1,test2,test3这个js怎么写?
meizz 2006-01-20
  • 打赏
  • 举报
回复
prototype.js 稍微看了一下,不妥之处与错误不少
ice_berg16 2006-01-20
  • 打赏
  • 举报
回复
使用prototype.js库可以更方便一些

<select name="text" onChange="test_change(this)">
<option value="">请选择</option>
<option value="t1">test1</option>
<option value="t1,t2">test2</option>
<option value="t1,t2,t3">test3</option>
</select>
<table>
<tr id="t1">
<td>test1</td>
</tr>

<tr id="t2">
<td>test2</td>
</tr>
<tr id="t3">
<td>test3</td>
</tr>
</table>
<script language="javascript" src="prototype.js"></script>
<script language="javascript">
<!--
Element.hide("t1","t2","t3");
function test_change(o){
Element.hide("t1","t2","t3");
Element.show.apply(window,o.value.split(","));
}
//-->
</script>

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧