zoukankan      html  css  js  c++  java
  • js实现自动给省市县地区自动赋值

    1.html部分:

    <span id="s_address_sheng">省</span>:
    <select name="address_sheng" onchange="change('address','_sheng','_shi','')" id="address_sheng" style="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="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="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="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;
    }

  • 相关阅读:
    MySQL注入总结
    使用JavaScript扫描端口
    dvwa+xampp搭建显示乱码的问题:解决办法
    精读《12 个评估 JS 库你需要关心的事》
    TinyMCE上传图片word
    xhEditor上传图片word
    JAVA大文件(100G以上)的上传下载实现技术
    JAVA大文件(1G以上)的上传下载实现技术
    java 支持 超大上G,多附件上传问题
    java 支持 超大上G,多附件上传方法
  • 原文地址:https://www.cnblogs.com/hupengyin/p/13279147.html
Copyright © 2011-2022 走看看