当我点击一个下拉列表内容,另一个下拉列表的内容会相应改变,应该怎样用PHP做? http://www.csdn.net/expert/topic/974/974562.xml?temp=.3960382这是我以前回答的一个问题.跟你一样.看看你会找到答案. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这不是PHP能做的,是要用javascript做的,PHP只是负责取数据,如果你一定要用PHP做,那也可以啊,刷新 TO phpteam(George):用javascript是怎样做呀?我不会呀!TO anziqi(安子琦):那篇文章很难看呀! <javascript>//¸Ãº¯ÊýÓÃÓÚ²úÉúÒ»¸öһάÊý×飬µ«Ò»°ãÓÃËü×öÑ­»·²úÉúÒ»¸öµÚÒ»¸öϱêΪÊýÖµ£¬µÚ¶þ¸öϱê¿ÉΪ'Data','Value','aText'µÄ¶þάÊý×é¡£ function aClass(aData, aValue, aText) { if (aClass.arguments.length < 3) alert("´«ÈëµÄ²ÎÊý´íÎó£¡") else { this.Data = aData; //²úÉúµÄ½«ÊǵÚÒ»¸öÏÂÀ­¿òµÄvalue this.Value = aValue; //²úÉúµÚ¶þ¸ö¿òµÄvalueÖµ this.Text = aText; //²úÉúµÚ¶þ¸ö¿òµÄtextÄÚÈÝ } }//¸Ãº¯ÊýÓÃÓڵõ½µÚÒ»¸öÏÂÀ­¿ò±»Ñ¡ÖжÔÏóµÄvalueÖµ//£¨aSrc=µÚÒ»¸öÏÂÀ­¿ò¶ÔÏó£»aDes=µÚ¶þ¸öÏÂÀ­¿ò¶ÔÏó£»aClassÓÃÉÏÃ溯Êý²úÉúµÄÊý×飻£© function chgDnCombobox(aSrc, aDes, aClass, aRemain){ if (chgDnCombobox.arguments.length < 3) alert("´«ÈëµÄ²ÎÊý´íÎó£¡") else if (aSrc.type != "select-one") //ÅжÏÏÂÀ­¿òÊDz»Êǵ¥Ñ¡¿ò alert("´«ÈëµÄ²ÎÊý´íÎó£¡") else if (aClass == null) alert("aClass䶨Òå´íÎó£¡") else { if (aRemain==null) aRemain = 1; chgDnComboboxItem(aSrc.options[aSrc.selectedIndex].value, aDes, aClass, aRemain); } } function chgDnComboboxItem(aValue, aDes, aClass, aRemain){ if (chgDnComboboxItem.arguments.length<3) alert("²ÎÊý´«Èë´íÎó£¡") else if (aClass==null) alert("´íÎó£¡aClassûÓж¨Ò壡") else{ if (aRemain==null) aRemain = 1; deleteAllComboboxItem(aDes, aRemain); //ɾ³ýµÚ¶þ¸öÏÂÀ­¿òµÄËùÓÐoption¶ÔÏó for (var i=0; i<aClass.length; i++) if (aClass[i].Data==aValue) /* check aClass[i].Data */ if (aClass[i].Data!="") addComboboxItem(aDes, aClass[i].Text, aClass[i].Value);//Éú³ÉµÚ¶þ¸öÏÂÀ­¿òµÄoption¶ÔÏó } }//ɾ³ýµÚ¶þ¸öÏÂÀ­¿òµÄËùÓÐoption¶ÔÏó function deleteAllComboboxItem(aList, aRemain) { for (var i=aList.options.length; i > aRemain; i--) aList.options[i] = null; } //Éú³ÉµÚ¶þ¸öÏÂÀ­¿òµÄoption¶ÔÏó function addComboboxItem(aList, aText, aValue) { var aOption = new Option(aText, aValue); aList.options[aList.options.length]=aOption; }///PHP拿数具var f_Set0 = new Array(<?phpfor($i=0;$i<$stateRow;$i++){ echo "new aClass('".$state->result[$i][country]."', '".$state->result[$i][state]."', '".$state->result[$i][state]."'),";} echo "new aClass('".$state->result[$i][country]."', '".$state->result[$i][state]."', '".$state->result[$i][state]."')"?>);function change_area(aForm) {chgDnCombobox(aForm.country, aForm.state, f_Set0 , 0)}</javascript>互动框<select name="country" onChange="change_area(document.dataform)"> <option value="NULL">--- <?php echo $Select;?> ---</option> <?php for ($i=0;$i<$countryRow;$i++) if ($currCompany->result[0][country]==$country->result[$i][country]) echo "<option value=\"".$country->result[$i][country]."\" selected>".$country->result[$i][description]."</option>"; else echo "<option value=\"".$country->result[$i][country]."\">".$country->result[$i][description]."</option>"; ?> </select>根据国家改变省洲,看见SQL部份是拿数据的,你要根据你自己的情况去拿这里主要用到的是javascript <select name="state"> <option value="NULL">--- <?php echo $Select;?> ---</option> <?php for ($i=0;$i<$stateRow;$i++) if ($currCompany->result[0][state]==$state->result[$i][state]) echo "<option value=\"".$state->result[$i][state]."\" selected>".$state->result[$i][state]."</option>"; else echo "<option value=\"".$state->result[$i][state]."\">".$state->result[$i][state]."</option>"; ?> </select> 这个也太烦了,我有简单很多的javascript程序 <? require("conn.php"); $sql="select * from table1"; $ro=mysql_query($sql); ?><html><head><title>新闻添加</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><script language="javascript"><!-- function check(){ if(document.form1.news_title.value=="") { alert("警告:\n没有填写新闻标题!") document.form1.news_title.select() return false } if(document.form1.news_sort.value==-1) { alert("警告:\n没有选择新闻分类!") document.form1.news_sort.focus() return false } if(document.form1.city.value==-1) { alert("警告:\n没有选择新闻二级分类!") document.form1.city.focus() return false } if(document.form1.news_message.value=="") { alert("警告:\n没有填写新闻内容!") document.form1.news_message.select() return false } document.form1.submit() return true } function cls(){ document.form1.news_title.value="" document.form1.news_author.value="" document.form1.news_message.value="" } function change_area(){var item_title = new Array;<?$j=0;while($rp=mysql_fetch_array($ro)){$kk= "\"".$rp["sort_name"]."&".$rp["sort_name1"]."\"\n";?>item_title[<?echo $j;?>]=<? echo $kk;?><?$j=$j+1;}?>if (document.form1.city.value!="-1"){ for(var i=0;i<document.form1.city.length;i++) { document.form1.city.options[0]= null; }}var menu;var j;j=0;for(var i=0;i<item_title.length;i++){ menu=item_title[i].split("&"); if(menu[0]==document.form1.news_sort.value) { document.form1.city.options[j]= new Option(menu[1],menu[1]); j++; }}if(j==0) { document.form1.city.options[0]= new Option("----请选择分类----","-1");}}//--></script><link rel="stylesheet" href="../../css/1.css" type="text/css"></head><body bgcolor="#cccccc" text="#000000" topmargin="0" leftmargin="0"><form name="form1" enctype="multipart/form-data" method="post" action="savenew.php?mode=ok" onSubmit="return check();"><?include("news_title.php")?> <table width="80%" border="0" cellspacing="0" cellpadding="0" align="center" class="word"> <tr> <td> <center> <b>新闻添加 </b></center> </td> <td rowspan="2"> </td> </tr> <tr> <td> <div align="right">说明:带<font color="#FF0000">*</font>为必填项</div> </td> </tr> <tr valign="top"> <td height="211"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="3%" height="214"> </td> <td width="97%" height="214" valign="top" bgcolor="#F7F7F7"> <table width="100%" border="1" bgcolor="#cccccc" bordercolorlight="#000000" cellspacing="0" bordercolordark="#FFFFFF" cellpadding="0" class="word"> <tr> <td width="19%"> <div align="center">新闻标题<font color="#FF0000">*</font>:</div> </td> <td width="76%"> <input type="text" name="news_title" size="50"> </td> </tr> <tr> <td width="19%"> <div align="center">新闻作者:</div> </td> <td width="76%"> <input type="text" name="news_author"> </td> </tr> <tr> <td width="19%"> <div align="center">新闻分类<font color="#FF0000">*</font>:</div> </td> <td width="76%"> <select name="news_sort" size=1 onChange="change_area()"> <option value="-1" selected>----请选择分类----</option> <? $sql="select * from table"; $ry=mysql_query($sql); while($rl=mysql_fetch_array($ry)) { ?> <option value="<?echo $rl["sort_id"];?>"> <?echo $rl["sort_name"];?> </option> <? } ?> </select> <a href="news_sort.php" class="word"><<<添加分类 </a> </td> </tr> <tr> <td width="19%" height="13"> <div align="center">二级分类<font color="#FF0000">*</font>:</div> </td> <td width="76%" height="13"> <select name="city" size=1 > <option value="-1" selected>----请选择分类----</option> </select> <a href="news_sort1.php" class="word"><<<添加二级分类</a> </td> </tr> <tr> <td width="19%" height="7"> <center> 新闻专集</center> </td> <td width="76%" height="7"> <select name="new_group"> <option value="">---请选择新闻专集---</option> <? $sqll="select group_name,group_code from news_group"; $rtk=mysql_query($sqll); while($rpl=mysql_fetch_array($rtk)) { ?> <option value="<?echo $rpl["group_code"];?>"><?echo $rpl["group_name"];?></option> <? } ?> </select> </td> </tr> <tr> <td width="19%" height="7"> <center> 新闻图片: </center> </td> <td width="76%" height="7"> <input type="file" name="pic" size="30"> </td> </tr> <tr> <td height="12" valign="top"> <div align="center">新闻内容<font color="#FF0000">*</font>:</div> <div align="center"> </div> </td> <td> <textarea name="news_message" cols="50" rows="9"></textarea> </td> </tr> <tr> <td width="19%"> <div align="center">首页显示:</div> </td> <td width="76%"> <input type="radio" name="news_on" value="0" checked> 显示 <input type="radio" name="news_on" value="1"> 不显示 </td> </tr> <tr> <td width="19%"> <center> 语 种: </center> </td> <td width="76%"> <input type="radio" name="lan" value="ch" checked> 中文 <input type="radio" name="lan" value="en"> 英文</td> </tr> </table> </td> </tr> </table> </td> <td height="211" width="5%"> </td> </tr> <tr> <td colspan="2"> <center> <input type="submit" name="Submit" value="添加"> <input type="reset" name="Submit2" value="取消"> </center> </td> </tr> </table></form></body></html> 如果数据量很大,建议用隐藏桢或者xmlhttp来取第二层数据 <form name=form1 action=""><select name="select1" onchange="document.form1.submit()"><option value="computer1">computer1</option><option value="computer2">computer2</option></select><?$sql=mysql_query("select * from table where computer='$select1'");$num=mysql_numrows($sql);if($num){?><select name="select2"><option value="字段值">字段值</option></select> <? } ?></form> 俺是第N次看到类似问题了,哈哈给你个结构清晰的参考,自己根据需要修改吧<select id=oSel1 onchange=Chg()><option> CPU<option> 硬盘<option> 主板</select><select id=oSel2></select><script>//二维数据表格(二维数组)var Table2dim = new Array( new Array("Intel PIII", "AMD Athlon", "Intel Celoron"), new Array("IBM", "Westwood", "Quantum"), new Array("Asus", "Gigabyte"), null);//Chg() 和 ChgOptions(i) 都是通用的,只要修改上面的 oSel1 的选项和 Table2dim 的内容即可function Chg(){ ChgOptions(document.all.oSel1.selectedIndex);}function ChgOptions(iSel1){ if ( 0 <= iSel1 && iSel1 < Table2dim.length ) { var oSel2 = document.all.oSel2, i; //删除所有旧选项(不知道有没有不用循环的删除办法) for ( i = oSel2.options.length; -- i >= 0; ) oSel2.options.remove(i); //创建新关联选项 var Table1dim = Table2dim[iSel1]; for ( i = 0; i < Table1dim.length; i ++ ) { var o = document.createElement('<option>'); o.text = Table1dim[i]; oSel2.options.add(o); } }}//初始化ChgOptions(0);</script> alexxing(赤铸) 说的很对上一次我就用的他的方法,很好的现在我还很感激他啊 俺没注意:是要用PHP做。前面一部分:<select id=oSel1 onchange=Chg()><option> CPU<option> 硬盘<option> 主板</select><select id=oSel2></select><script>//二维数据表格(二维数组)var Table2dim = new Array( new Array("Intel PIII", "AMD Athlon", "Intel Celoron"), new Array("IBM", "Westwood", "Quantum"), new Array("Asus", "Gigabyte"), null);可以用PHP从数据库中取出数据动态生成(把后面的函数放到单独的.JS文件中,可以提高速度)我的方法只适合于数据量有限的情况,但是简单如果你有几百种零配件,几百种CPU,就最好用PHP动态交互实现SELECT的更新,这就复杂多了,需要用一个隐藏帧在“幕后”与服务器交互取得数据,再用DHTML更新SELECT的内容 preg_replace 不支持元字符"\b"??? 非常奇怪的问题 向高手求助啊..... 请问如果需要在火狐下运行该如何解决 二级域名问题 送分:是否有函数支持把字符转成&#xxxx; ◇◆热心侠客请进!!!一个坚硬的问题,在线等你!!◇◆ 在网上下载了两个php系统的原代码,怎么使他们共用同一用户数据,详情请进:) 寻一程序 又发现有新问题啦~~~~~~大家进来! php中的class可以对方法进行重载(override)吗? 一个奇怪的问题. PHP可以编写WEB应用程序吗?
//¸Ãº¯ÊýÓÃÓÚ²úÉúÒ»¸öһάÊý×飬µ«Ò»°ãÓÃËü×öÑ­»·²úÉúÒ»¸öµÚÒ»¸öϱêΪÊýÖµ£¬µÚ¶þ¸öϱê¿ÉΪ'Data','Value','aText'µÄ¶þάÊý×é¡£
function aClass(aData, aValue, aText) {
if (aClass.arguments.length < 3)
alert("´«ÈëµÄ²ÎÊý´íÎó£¡")
else {
this.Data = aData; //²úÉúµÄ½«ÊǵÚÒ»¸öÏÂÀ­¿òµÄvalue
this.Value = aValue; //²úÉúµÚ¶þ¸ö¿òµÄvalueÖµ
this.Text = aText; //²úÉúµÚ¶þ¸ö¿òµÄtextÄÚÈÝ
}
}
//¸Ãº¯ÊýÓÃÓڵõ½µÚÒ»¸öÏÂÀ­¿ò±»Ñ¡ÖжÔÏóµÄvalueÖµ
//£¨aSrc=µÚÒ»¸öÏÂÀ­¿ò¶ÔÏó£»aDes=µÚ¶þ¸öÏÂÀ­¿ò¶ÔÏó£»aClassÓÃÉÏÃ溯Êý²úÉúµÄÊý×飻£©
function chgDnCombobox(aSrc, aDes, aClass, aRemain){
if (chgDnCombobox.arguments.length < 3)
alert("´«ÈëµÄ²ÎÊý´íÎó£¡")
else if (aSrc.type != "select-one") //ÅжÏÏÂÀ­¿òÊDz»Êǵ¥Ñ¡¿ò
alert("´«ÈëµÄ²ÎÊý´íÎó£¡")
else if (aClass == null)
alert("aClass䶨Òå´íÎó£¡")
else {
if (aRemain==null)
aRemain = 1;
chgDnComboboxItem(aSrc.options[aSrc.selectedIndex].value, aDes, aClass, aRemain);
}
} function chgDnComboboxItem(aValue, aDes, aClass, aRemain){
if (chgDnComboboxItem.arguments.length<3)
alert("²ÎÊý´«Èë´íÎó£¡")
else if (aClass==null)
alert("´íÎó£¡aClassûÓж¨Ò壡")
else{
if (aRemain==null)
aRemain = 1;
deleteAllComboboxItem(aDes, aRemain); //ɾ³ýµÚ¶þ¸öÏÂÀ­¿òµÄËùÓÐoption¶ÔÏó
for (var i=0; i<aClass.length; i++)
if (aClass[i].Data==aValue) /* check aClass[i].Data */
if (aClass[i].Data!="")
addComboboxItem(aDes, aClass[i].Text, aClass[i].Value);//Éú³ÉµÚ¶þ¸öÏÂÀ­¿òµÄoption¶ÔÏó
}
}
//ɾ³ýµÚ¶þ¸öÏÂÀ­¿òµÄËùÓÐoption¶ÔÏó
function deleteAllComboboxItem(aList, aRemain) {
for (var i=aList.options.length; i > aRemain; i--)
aList.options[i] = null;
}
//Éú³ÉµÚ¶þ¸öÏÂÀ­¿òµÄoption¶ÔÏó
function addComboboxItem(aList, aText, aValue) {
var aOption = new Option(aText, aValue);
aList.options[aList.options.length]=aOption; }
///PHP拿数具
var f_Set0 = new Array(
<?php
for($i=0;$i<$stateRow;$i++)
{
echo "new aClass('".$state->result[$i][country]."', '".$state->result[$i][state]."', '".$state->result[$i][state]."'),";
}
echo "new aClass('".$state->result[$i][country]."', '".$state->result[$i][state]."', '".$state->result[$i][state]."')"
?>
);
function change_area(aForm) {
chgDnCombobox(aForm.country, aForm.state, f_Set0 , 0)
}</javascript>互动框
<select name="country" onChange="change_area(document.dataform)">
<option value="NULL">---
<?php echo $Select;?>
---</option>
<?php
for ($i=0;$i<$countryRow;$i++)
if ($currCompany->result[0][country]==$country->result[$i][country])
echo "<option value=\"".$country->result[$i][country]."\" selected>".$country->result[$i][description]."</option>";
else
echo "<option value=\"".$country->result[$i][country]."\">".$country->result[$i][description]."</option>";
?>
</select>根据国家改变省洲,看见SQL部份是拿数据的,你要根据你自己的情况去拿
这里主要用到的是javascript
<select name="state">
<option value="NULL">---
<?php echo $Select;?>
---</option>
<?php
for ($i=0;$i<$stateRow;$i++)
if ($currCompany->result[0][state]==$state->result[$i][state])
echo "<option value=\"".$state->result[$i][state]."\" selected>".$state->result[$i][state]."</option>";
else
echo "<option value=\"".$state->result[$i][state]."\">".$state->result[$i][state]."</option>";
?>
</select>
require("conn.php");
$sql="select * from table1";
$ro=mysql_query($sql);
?>
<html>
<head>
<title>新闻添加</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">
<!--
function check(){
if(document.form1.news_title.value=="")
{
alert("警告:\n没有填写新闻标题!")
document.form1.news_title.select()
return false
}
if(document.form1.news_sort.value==-1)
{
alert("警告:\n没有选择新闻分类!")
document.form1.news_sort.focus()
return false
}
if(document.form1.city.value==-1)
{
alert("警告:\n没有选择新闻二级分类!")
document.form1.city.focus()
return false
}
if(document.form1.news_message.value=="")
{
alert("警告:\n没有填写新闻内容!")
document.form1.news_message.select()
return false
}
document.form1.submit()
return true
}
function cls()
{
document.form1.news_title.value=""
document.form1.news_author.value=""
document.form1.news_message.value=""
}
function change_area()
{
var item_title = new Array;
<?
$j=0;
while($rp=mysql_fetch_array($ro))
{
$kk= "\"".$rp["sort_name"]."&".$rp["sort_name1"]."\"\n";
?>
item_title[<?echo $j;?>]=<? echo $kk;?>
<?
$j=$j+1;
}
?>
if (document.form1.city.value!="-1")
{
for(var i=0;i<document.form1.city.length;i++)
{
document.form1.city.options[0]= null;
}
}
var menu;
var j;
j=0;
for(var i=0;i<item_title.length;i++)
{
menu=item_title[i].split("&");
if(menu[0]==document.form1.news_sort.value)
{
document.form1.city.options[j]= new Option(menu[1],menu[1]);
j++;
}
}
if(j==0)
{
document.form1.city.options[0]= new Option("----请选择分类----","-1");
}
}
//-->
</script><link rel="stylesheet" href="../../css/1.css" type="text/css">
</head><body bgcolor="#cccccc" text="#000000" topmargin="0" leftmargin="0">
<form name="form1" enctype="multipart/form-data" method="post" action="savenew.php?mode=ok" onSubmit="return check();">
<?include("news_title.php")?>
<table width="80%" border="0" cellspacing="0" cellpadding="0" align="center" class="word">
<tr>
<td>
<center>
<b>新闻添加 </b>
</center>
</td>
<td rowspan="2"> </td>
</tr>
<tr>
<td>
<div align="right">说明:带<font color="#FF0000">*</font>为必填项</div>
</td>
</tr>
<tr valign="top">
<td height="211">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="3%" height="214"> </td>
<td width="97%" height="214" valign="top" bgcolor="#F7F7F7">
<table width="100%" border="1" bgcolor="#cccccc" bordercolorlight="#000000" cellspacing="0" bordercolordark="#FFFFFF" cellpadding="0" class="word">
<tr>
<td width="19%">
<div align="center">新闻标题<font color="#FF0000">*</font>:</div>
</td>
<td width="76%">
<input type="text" name="news_title" size="50">
</td>
</tr>
<tr>
<td width="19%">
<div align="center">新闻作者:</div>
</td>
<td width="76%">
<input type="text" name="news_author">
</td>
</tr>
<tr>
<td width="19%">
<div align="center">新闻分类<font color="#FF0000">*</font>:</div>
</td>
<td width="76%">
<select name="news_sort" size=1 onChange="change_area()">
<option value="-1" selected>----请选择分类----</option>
<?
$sql="select * from table";
$ry=mysql_query($sql);
while($rl=mysql_fetch_array($ry))
{
?>
<option value="<?echo $rl["sort_id"];?>">
<?echo $rl["sort_name"];?>
</option>
<?
}
?>
</select>
<a href="news_sort.php" class="word"><<<添加分类 </a> </td>
</tr>
<tr>
<td width="19%" height="13">
<div align="center">二级分类<font color="#FF0000">*</font>:</div>
</td>
<td width="76%" height="13">
<select name="city" size=1 >
<option value="-1" selected>----请选择分类----</option>
</select>
<a href="news_sort1.php" class="word"><<<添加二级分类</a> </td>
</tr>
<tr>
<td width="19%" height="7">
<center>
新闻专集
</center>
</td>
<td width="76%" height="7">
<select name="new_group">
<option value="">---请选择新闻专集---</option>
<?
$sqll="select group_name,group_code from news_group";
$rtk=mysql_query($sqll);
while($rpl=mysql_fetch_array($rtk))
{
?>
<option value="<?echo $rpl["group_code"];?>"><?echo $rpl["group_name"];?></option>
<?
}
?>
</select>
</td>
</tr>
<tr>
<td width="19%" height="7">
<center>
新闻图片:
</center>
</td>
<td width="76%" height="7">
<input type="file" name="pic" size="30">
</td>
</tr>
<tr>
<td height="12" valign="top">
<div align="center">新闻内容<font color="#FF0000">*</font>:</div>
<div align="center"> </div>
</td>
<td>
<textarea name="news_message" cols="50" rows="9"></textarea>
</td>
</tr>
<tr>
<td width="19%">
<div align="center">首页显示:</div>
</td>
<td width="76%">
<input type="radio" name="news_on" value="0" checked>
显示
<input type="radio" name="news_on" value="1">
不显示 </td>
</tr>
<tr>
<td width="19%">
<center>
语 种:
</center>
</td>
<td width="76%">
<input type="radio" name="lan" value="ch" checked>
中文
<input type="radio" name="lan" value="en">
英文</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td height="211" width="5%"> </td>
</tr>
<tr>
<td colspan="2">
<center>
<input type="submit" name="Submit" value="添加">
<input type="reset" name="Submit2" value="取消">
</center>
</td>
</tr>
</table>
</form></body>
</html>
<select name="select1" onchange="document.form1.submit()">
<option value="computer1">computer1</option>
<option value="computer2">computer2</option>
</select>
<?
$sql=mysql_query("select * from table where computer='$select1'");
$num=mysql_numrows($sql);
if($num){
?>
<select name="select2">
<option value="字段值">字段值</option>
</select>
<?
}
?>
</form>
<option> CPU
<option> 硬盘
<option> 主板
</select>
<select id=oSel2>
</select><script>//二维数据表格(二维数组)
var Table2dim = new Array(
new Array("Intel PIII", "AMD Athlon", "Intel Celoron"),
new Array("IBM", "Westwood", "Quantum"),
new Array("Asus", "Gigabyte"),
null
);//Chg() 和 ChgOptions(i) 都是通用的,只要修改上面的 oSel1 的选项和 Table2dim 的内容即可
function Chg()
{
ChgOptions(document.all.oSel1.selectedIndex);
}
function ChgOptions(iSel1)
{
if ( 0 <= iSel1 && iSel1 < Table2dim.length )
{
var oSel2 = document.all.oSel2, i; //删除所有旧选项(不知道有没有不用循环的删除办法)
for ( i = oSel2.options.length; -- i >= 0; )
oSel2.options.remove(i); //创建新关联选项
var Table1dim = Table2dim[iSel1];
for ( i = 0; i < Table1dim.length; i ++ )
{
var o = document.createElement('<option>');
o.text = Table1dim[i];
oSel2.options.add(o);
}
}
}//初始化
ChgOptions(0);
</script>
上一次我就用的他的方法,很好的
现在我还很感激他啊
<option> CPU
<option> 硬盘
<option> 主板
</select>
<select id=oSel2>
</select><script>//二维数据表格(二维数组)
var Table2dim = new Array(
new Array("Intel PIII", "AMD Athlon", "Intel Celoron"),
new Array("IBM", "Westwood", "Quantum"),
new Array("Asus", "Gigabyte"),
null
);可以用PHP从数据库中取出数据动态生成
(把后面的函数放到单独的.JS文件中,可以提高速度)我的方法只适合于数据量有限的情况,但是简单如果你有几百种零配件,几百种CPU,就最好用PHP动态交互实现SELECT的更新,这就复杂多了,需要用一个隐藏帧在“幕后”与服务器交互取得数据,再用DHTML更新SELECT的内容