关闭

三级联动效果

458人阅读 评论(0) 收藏 举报

大家共同进步,共同学习,现把代码贴下,希望能够起到抛砖引玉的效果。


<script language="javascript">
function getcity(objcity,objprov,objplace,defcity,defprov,defplace)
{
while(objcity.options != null && objcity.options.length>0)
{
objcity.options.remove(0);
}
var oOptionFirst= document.createElement("OPTION");
objcity.options.add(oOptionFirst);
oOptionFirst.value = '';
oOptionFirst.innerText = '请选择';
if(defcity == '')
{
oOptionFirst.selected=true;
}
for(i=0;i<city.length;i++)
{
var oOption= document.createElement("OPTION");
objcity.options.add(oOption);
oOption.value = city[i];
oOption.innerText = city[i];
if(city[i] == defcity)
{
oOption.selected = true;
}
}
chgprov(objcity,objprov,objplace,defprov,defplace);
}

function chgprov(objcity,objprov,objplace,defprov,defplace)
{
idx=objcity.selectedIndex-1;
while(objprov.options != null && objprov.options.length>0)
{
objprov.options.remove(0);
}
var oOptionFirst= document.createElement("OPTION");
objprov.options.add(oOptionFirst);
oOptionFirst.value = '';
oOptionFirst.innerText = '请选择';
if(defprov == '')
{
oOptionFirst.selected=true;
}
if(idx >= 0)
{
for(i=0;i<eval('prov'+idx).length;i++)
{
var oOption= document.createElement("OPTION");
objprov.options.add(oOption);
oOption.value = eval('prov'+idx+'[i]');
oOption.innerText = eval('prov'+idx+'[i]');
if(eval('prov'+idx+'[i]') == defprov)
{
oOption.selected = true;
}
}
}
chgplace(objcity,objprov,objplace,defplace);
}


function chgplace(objcity,objprov,objplace,defplace)
{
pidx = objcity.selectedIndex-1;
idx=objprov.selectedIndex-1;
while(objplace.options != null && objplace.options.length>0)
{
objplace.options.remove(0);
}
var oOptionFirst= document.createElement("OPTION");
objplace.options.add(oOptionFirst);
oOptionFirst.value = '';
oOptionFirst.innerText = '请选择';
if(defplace == '')
{
oOptionFirst.selected=true;
}
if(idx >= 0)
{
for(i=0;i<eval('place'+pidx+'_'+idx).length;i++)
{
var oOption= document.createElement("OPTION");
objplace.options.add(oOption);
oOption.value = eval('place'+pidx+'_'+idx+'[i]');
oOption.innerText = eval('place'+pidx+'_'+idx+'[i]');
if(eval('place'+pidx+'_'+idx+'[i]') == defplace)
{
oOption.selected = true;
}
}
}
}
</script>
<form id="form1" method="post" runat="server">
<table>
<tr>
<td width="90" height="24"

align="right"><font color="666666">位置:</font></td>
<td width="90" align="left">
<select name="city" class="p3"

size="1" onChange="chgprov(this,form1.province,form1.place,'','')">
</select></td>
<td width="90" align="left">
<select name="province" class="p3"

size="1" onChange="chgplace(form1.city,this,form1.place,'')">
</select></td>
<td width="119"><select name="place"

class="p3">
</select>
<script>
                            getcity(form1.city,form1.province,form1.place,'','','')
</script>
</td>
</tr>
</table>
</form>


这是后台页面,在page_load 中调用输出就可以 Response.write(getJSContent());
public string getJSContent()
{
SqlConnection conn = new SqlConnection(strconn);
conn.Open();

string strCity = " var city = new Array(" ;
string strProv = "" ;
string strPlace = "" ;
DataTable dt = new DataTable();
string strsql = "select * from xbzy" ;
SqlDataAdapter da = new SqlDataAdapter(strsql,conn);
DataSet ds = new DataSet();
da.Fill(ds);
dt = ds.Tables[0];
for(int i = 0 ;i<dt.Rows.Count ;i++)
{
strCity += "'" + dt.Rows[i][1].ToString().Trim() + "'," ;
string sqli = "select * from xbzy where id = " + dt.Rows[i]

[0].ToString().Trim() ;
SqlDataAdapter dai = new SqlDataAdapter(sqli,conn);
DataSet dsi = new DataSet();
dai.Fill(dsi);
DataTable dti = dsi.Tables[0];
strProv += " var prov" + i + " = new Array(" ;
for(int j = 0 ;j<dti.Rows.Count ; j++)
{
strProv += "'" + dti.Rows[j][1].ToString().Trim() +

"'," ;
string sqlj = "select * from xbzy where id = " +

dti.Rows[j][0].ToString().Trim() ;
SqlDataAdapter daj = new SqlDataAdapter(sqlj,conn);
DataSet dsj = new DataSet();
daj.Fill(dsj);
DataTable dtj = dsj.Tables[0];
strPlace += " var place" + i + "_" + j + " = new

Array(" ;
for(int m = 0 ;m<dtj.Rows.Count ; m++)
{
strPlace += "'" + dtj.Rows[m][1].ToString

().Trim() + "'," ;
}
if(strPlace.EndsWith(","))
{
strPlace = strPlace.Substring

(0,strPlace.Length-1);
}
strPlace += ");/n/r" ;
}
if(strProv.EndsWith(","))
{
strProv = strProv.Substring(0,strProv.Length-1);
}
strProv += ");/n/r" ;
}
if(strCity.EndsWith(","))
{
strCity = strCity.Substring(0,strCity.Length-1);
}
strCity += ");/n/r" ;
conn.Close();
return "<script language='javascript'>/n/r" + strCity + strProv +

strPlace + "</script>" ;
}


有的还不是很完美,还请高手指点!!!

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11089次
    • 积分:218
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:6篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论