js实现自动给省市县地区自动赋值

Posted 阳雪凌空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js实现自动给省市县地区自动赋值相关的知识,希望对你有一定的参考价值。

1.html部分:

<span id="s_address_sheng">省</span>:
<select name="address_sheng" onchange="change(‘address‘,‘_sheng‘,‘_shi‘,‘‘)" id="address_sheng" style="width:80px;" >
<option value="">--请选择--</option>
<?php
foreach ($res_sheng as $key => $value) {

?>
<option value="<?php echo $value[‘code‘]?>" <?php if($value[‘code‘]==$resmes[‘address_sheng‘] ){echo ‘selected="selected"‘;}else if($resmes[‘address_sheng‘] ==‘‘ && $value[‘code‘]==$sheng_mo){echo ‘selected="selected"‘;}?>><?php echo $value[‘name‘];?></option>

<?php
}
?>
</select>

<span id="s_address_shi">市</span>:
<select name="address_shi" onchange="change(‘address‘,‘_shi‘,‘_xian‘,‘‘)" id="address_shi" style="width:80px" >
<option value="">--请选择市区--</option>
<?php
foreach ($res_shi as $key => $value) {

?>
<option value="<?php echo $value[‘code‘]?>" <?php if($value[‘code‘]==$resmes[‘address_shi‘]){echo ‘selected="selected"‘;}else if($resmes[‘address_shi‘] ==‘‘ && $value[‘code‘]==$shi_mo){echo ‘selected="selected"‘;}?>><?php echo $value[‘name‘];?></option>

<?php
}
?>
</select>

<span id="s_address_xian">县(区)</span>:
<select name="address_xian" onchange="change(‘address‘,‘_xian‘,‘_jie‘,‘‘)" id="address_xian" style="width:80px" >
<option value="">--请选择县区--</option>
<?php
foreach ($res_xian as $key => $value) {

?>

<option value="<?php echo $value[‘code‘]?>" <?php if($value[‘code‘]==$resmes[‘address_xian‘]){echo ‘selected="selected"‘;}else if($resmes[‘address_xian‘] ==‘‘ && $value[‘code‘]==$xian_mo){echo ‘selected="selected"‘;}?>><?php echo $value[‘name‘];?></option>

<?php
}
?>
</select>

<span id="s_address_jie">乡(镇、街道)</span>:
<select name="address_jie" onChange="changejie()" id="address_jie" style="width:120px">
<option value="">--请选择街道--</option>
<?php
foreach ($res_jie as $key => $value) {

?>
<option value="<?php echo $value[‘code‘]?>" <?php if($value[‘code‘]==$resmes[‘address_xiang‘]){echo ‘selected="selected"‘;}else if($resmes[‘address_xiang‘] ==‘‘ && $value[‘code‘]==$jie_mo){echo ‘selected="selected"‘;}?>><?php echo $value[‘name‘];?></option>

<?php
}
?>
</select>

2.js部分:

$(document).ready(function(){
$("#address_sheng").val("{$resmes[‘address_sheng‘]}");

$("#address_sheng_h").val("{$resmes[‘address_sheng‘]}");
$("#address_shi_h").val("{$resmes[‘address_shi‘]}");
$("#address_xian_h").val("{$resmes[‘address_xian‘]}");
$("#address_jie_h").val("{$resmes[‘address_jie‘]}");

change(‘address‘,‘_sheng‘,‘_shi‘,"");

$("#address_cun").val("{$resmes[‘address_cun‘]}");
$("#address_men").val("{$resmes[‘address_men‘]}");

$("#address_xx").val("{$resmes[‘address_xx‘]}");

 

});

3.php 后端代码部分:

$type = $_POST[‘type‘];
if($_POST[‘type‘] ==‘_sheng‘){
$where = " provincecode = ‘".$_POST["pro_code"]."‘";
$resultshi = $Dict->City(‘‘,$where);

$shi = "<option name=‘".$type."‘ value=‘‘>--请选择市区--</option>";
foreach($resultshi as $key=>$val){
$shi .= "<option name=‘".$type."‘ value=‘{$val[‘code‘]}‘ >{$val[‘name‘]}</option>";
}
echo json_encode($shi);die;
}
if($_POST[‘type‘] ==‘_shi‘){
// $sqlxian = "select * from area where citycode=‘".$_POST["shi_code"]."‘";
// $resultxian = M()->query($sqlxian);

$where = " citycode=‘".$_POST["pro_code"]."‘";
$resultxian = $Dict->Area(‘‘,$where);

$xian = "<option name=‘".$type."‘ value=‘‘>--请选择县区--</option>";
foreach($resultxian as $key=>$val){
$xian .= "<option name=‘".$type."‘ value=‘{$val[‘code‘]}‘>{$val[‘name‘]}</option>";
}
echo json_encode($xian);die;
}
if($_POST[‘type‘] ==‘_xian‘){
$where = " where areacode=‘".$_POST["pro_code"]."‘";
$sql = "select * from road ".$where;
$resultjie =M()->query($sql);

$jie = "<option value=‘‘>--请选择街道--</option>";
foreach($resultjie as $key=>$val){
$jie .= "<option name=‘".$type."‘ value=‘{$val[‘code‘]}‘>".$val[‘name‘]."</option>";
}
echo json_encode($jie);die;
}

以上是关于js实现自动给省市县地区自动赋值的主要内容,如果未能解决你的问题,请参考以下文章

Jsoup获取全国地区数据(省市县镇村)

全国省市县三级地区的数据库存储语句

省市县三级联动(jqurey+json)

地区选择

添加行政地区编码

vue+element省市县的二级联动功能