用Ajax写的省市联动,添加地址的时候默认的省市都显示,- -请选择- - 但是修改的时候如何让省市下拉菜单里默认的选中,地址里的省市?[下拉菜单里的省,是直接从数据库拿出来保存在一个数组变量里,把变量付给下拉框]
js代码:
html代码:
php文件:
<?php
require_once (dirname(__FILE__) . "/../include/common.inc.php");
require_once(DEDEINC.'/dedesql.class.php');
//服务器端
//告诉浏览器返回的数据格式是xml格式
header("Content-Type: text/xml;charset=utf-8");
//告诉浏览器不要缓存数据
header("Cache-Control: no-cache");
//接收用户选择省的名字
$province=$_POST['province'];
$city = array();
$dsql->SetQuery("SELECT * FROM area WHERE reid=$province");
$dsql->Execute();
$i = 0 ;
while($row=$dsql->GetArray())
{
$city[] = $row;
$i++;
}
unset($row);
$info="
$len=count($city);
for($l=0;$l //file_put_contents("e:/aa.txt",$city[$l][name],FILE_APPEND);
$info.="
}
$info.="
file_put_contents("e:/aa.txt",$info,FILE_APPEND);
echo $info;
回复讨论(解决方案)
选中 省 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();
}