码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2020-07-10 15:39:05      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:city   function   HERE   val   sheng   address   option   from   mes   

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实现自动给省市县地区自动赋值

标签:city   function   HERE   val   sheng   address   option   from   mes   

原文地址:https://www.cnblogs.com/hupengyin/p/13279147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!