yskl 发表于 2018-2-11 12:07:56


            本文实例讲述了Yii2使用dropdownlist实现地区三级联动功能的方法。分享给大家供大家参考,具体如下:
视图部分:

   ['index'],
      'method' => 'get',
      'options' => ['class' => 'form-inline']
    ]); ?>
    field($model, 'cityName', ['options' => ['class' => 'form-group col-lg-2']])->dropDownList(ArrayHelper::map($cities, 'id', 'name'), ['prompt' => '请选择城市'])->label('请选择城市', ['class' => 'sr-only']) ?>
    field($model, 'areaName', ['options' => ['class' => 'form-group col-lg-2']])->dropDownList(ArrayHelper::map($areas, 'id', 'name'), ['prompt' => '请选择区县'])->label('请选择区县', ['class' => 'sr-only']) ?>
    field($model, 'communityName', ['options' => ['class' => 'form-group col-lg-2']])->dropDownList(ArrayHelper::map($communities, 'id', 'name'), ['prompt' => '请选择小区'])->label('请选择小区', ['class' => 'sr-only']) ?>
   
      
   
   
      搜索
   
   
   
       'btn btn-primary']) ?>
   
   


registerJs('
//市地址改变
$("#itemsearch-cityname").change(function() {
    //市id值
    var cityid = $(this).val();
    $("#itemsearch-areaname").html("请选择区县");
    $("#itemsearch-communityname").html("请选择小区");
    if (cityid > 0) {
      getArea(cityid);
    }
});
//区地址改变
$("#itemsearch-areaname").change(function() {
    //区id值
    var areaid = $(this).val();
    $("#itemsearch-communityname").html("请选择小区");
    if (areaid > 0) {
      getCommunity(areaid);
    }
});
//获取市下面的区列表
function getArea(id)
{
    var href = "' . Url::to(['/service/base/get-area-list'], true). '";
    $.ajax({
      "type" : "GET",
      "url": href,
      "data" : {id : id},
      success : function(d) {
      $("#itemsearch-areaname").append(d);
      }
    });
}
//获取区下面的小区列表
function getCommunity(id)
{
    var href = "' . Url::to(['/service/base/get-community-list'], true) . '";
    $.ajax({
      "type" : "GET",
      "url": href,
      "data" : {id : id},
      success : function(d) {
      $("#itemsearch-communityname").append(d);
      }
    });
}
//搜索小区
$("#search-community").click(function() {
    var word= $("#keyword").val();
    var areaid = $("#itemsearch-areaname option:selected").val();
    var href= "' . Url::to(['/service/base/search-community'], true) . '";
    if (areaid > 0) {
      $.ajax({
      "type" : "GET",
      "url": href,
      "data" : {id : areaid, word : word},
      success : function(d) {
          $("#itemsearch-communityname").html(d);
      }
      });
    }
});
');
?>
页: [1]
查看完整版本: Yii2使用dropdownlist实现地区三级联动功能的方法