Heim >Backend-Entwicklung >PHP-Tutorial >用Ajax写的省市联动,修改时如何显示地址里已有的省和市
用Ajax写的省市联动,添加地址的时候默认的省市都显示,- -请选择- - 但是修改的时候如何让省市下拉菜单里默认的选中,地址里的省市?[下拉菜单里的省,是直接从数据库拿出来保存在一个数组变量里,把变量付给下拉框]
js代码:
<script> <br /> //创建Ajax引擎对象 <br /> function getXmlHttpObject(){ <br /> var xmlHttpRequest; <br /> if(window.ActiveXObject){ <br /> xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP"); <br /> }else{ <br /> xmlHttpRequest=new XMLHttpRequest(); <br /> } <br /> return xmlHttpRequest; <br /> } <br /> <br /> var myXmlHttpRequest=""; <br /> <br /> <br /> <br /> function getCities(){ <br /> <br /> myXmlHttpRequest=getXmlHttpObject(); <br /> <br /> if(myXmlHttpRequest){ <br /> <br /> var url="../plus/showcity.php";//post <br /> <br /> var data="province="+$("sheng").value; <br /> <br /> myXmlHttpRequest.open("post",url,true); <br /> <br /> myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); <br /> <br /> myXmlHttpRequest.onreadystatechange=chuli; <br /> <br /> myXmlHttpRequest.send(data); <br /> <br /> <br /> } <br /> } <br /> <br /> function chuli(){ <br /> <br /> if(myXmlHttpRequest.readyState==4){ <br /> <br /> if(myXmlHttpRequest.status==200){ <br /> //取出服务器回送的数据 <br /> //window.alert(myXmlHttpRequest.responseXML); <br /> var cities=myXmlHttpRequest.responseXML.getElementsByTagName("city"); <br /> //alert(cities.length); <br /> //遍历并取出城市 <br /> $("city").length=0; <br /> var myOption=document.createElement("option"); <br /> myOption.innerHTML="--请选择--"; <br /> $("city").appendChild(myOption); <br /> <br /> for(var i=0;i<cities.length;i++){ <br /> var city_name=cities[i].childNodes[0].nodeValue; <br /> //alert(city_name); <br /> var myOption=document.createElement("option"); <br /> myOption.value=city_name; <br /> myOption.innerHTML=city_name; <br /> $("city").appendChild(myOption); <br /> } <br /> } <br /> } <br /> } <br /> <br /> function $(id){ <br /> <br /> return document.getElementById(id); <br /> } <br /> </script>
html代码:
选中 省 ajax 取得 市 列表
并不需要再修改 省 列表了
怎么选中省呢??
那我就奇怪了,你不是 省市联动 吗?
选中省 当然是用户的事情啦
Ajax实现数据库填充省市下拉框联动菜单示例
//Ajax和DropDownList设置
//调用数据库显示下拉框数据
SqlConnection sqlcon;
string strCon = ConfigurationManager.AppSettings["conn"];
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "select tbSheng from tb_Sheng";
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds);
dltbSheng.DataSource= myds;
dltbSheng.DataValueField = "tbSheng";
dltbSheng.DataBind();
string strtb_Shi = "select * from dl_Shi where tbSheng='" + dltbSheng.SelectedItem.Text + "'";
SqlDataAdapter mydaShi = new SqlDataAdapter(strtb_Shi, sqlcon);
DataSet mydsShi = new DataSet();
mydaShi.Fill(mydsShi);
ddlShi.DataSource = mydsShi;
ddlShi.DataValueField = "tb_Shi";
ddlShi.DataBind();
sqlcon.Close();
}
}
protected void dlSheng_SelectedIndexChanged(object sender, EventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "select * from dl_Shi where tbSheng='" + dltbSheng.SelectedItem.Text+ "'";
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds);
ddlShi.DataSource = myds;
ddlShi.DataValueField = "tb_Shi";
ddlShi.DataBind();
sqlcon.Close();
}