okokad 2016-08-11 09:19:42 5412次浏览 4条评论 13 2 0

表字段:
id,name,parent
模型添加:

public function getCityList($pid=0)
{
    $model = Area::findAll(array('parent'=>$pid));
    $arr= ArrayHelper::map($model, 'name', 'id');
    $arr=array_merge(['请选择' =>'0'],$arr);
    $arr=array_flip($arr);
    return $arr;
}

控制器:

public function actionSite($pid=0)
{
    $model = new Area();
    $model = $model->getCityList($pid);
    foreach($model as $value=>$name)
    {
        echo Html::tag('option',Html::encode($name),array('value'=>$value));
    }
}

FORM:

<?=$form->field($model, 'parent')->dropDownList(['1'=>'广东','2'=>'北京'],['style'=>'width:auto','class'=>"form-control link"])?>

JS:

$(function(){
	links($('.link'),'Area[parent]');
});
function links(obj,name){
	obj.change(function(){
		var o=$("select[name='Area[parent]']");
		o.attr({"name":''});
		obj.attr({"name":name})
		obj.nextAll().remove();
		if($(this).val()==0)return false;
		$.post('?r=area/site&pid='+$(this).val(),function(data){
			if(data!='<option value="0">请选择</option>'){
				var obc=$("<select>").insertAfter(obj).html(data).addClass('form-control link');
				links(obc,name);
			}
	    });
	});
}

欢迎指导,提意见。

觉得很赞
  • 评论于 2016-08-11 09:57 举报

    楼主一定要用markdown格式书写,否则会特别乱,我已经帮你修改好了,以后要注意。

    1 条回复
    评论于 2016-08-11 13:17 回复

    好的。谢谢。

  • 评论于 2016-08-16 22:13 举报

    dfasd

    1 条回复
    评论于 2016-08-28 20:48 回复

    爱我爱爱爱爱

  • 评论于 2016-08-28 20:47 举报

    不错哦不错哦

    1 条回复
    评论于 2016-08-28 20:47 回复

    是吗是那是那么是按是吗

  • 评论于 2016-09-07 14:04 举报

    看看我这个合适不,哪里出错了?
    `

            <label for="community-country_id">地区</label>
            <div>
                <?php
                $countryList = array();
                $countryList = ArrayHelper::map(CountryRegion::find()->all(), 'id', 'name');
                ?>
                <select id="community-country_id" class="form-control" name="UserContact[country_id]" style="width:190px;display: inline-block">
                    <option value="" selected>请选择</option>
                    <?php foreach ($countryList as $key=>$val):?>
                        <option value="<?= $key ?>" <?=$modelContact->country_id != null && $modelContact->country_id == $key?' selected ':'' ?>><?= $val ?></option>
                    <?php endforeach; ?>
                </select>
                <?php
                $provinceList = array();
                if ($modelContact->country_id) {
                    $provinceList = ArrayHelper::map(Province::find()->where(['country_id' => $modelContact->country_id])->all(), 'id', 'name');
                }
                ?>
                <select id="community-province_id" class="form-control" name="UserContact[province_id]" style="width:190px;display: inline-block">
                    <option value="">请选择</option>
                    <?php foreach ($provinceList as $key=>$val): ?>
                        <option value="<?= $key ?>"<?=$modelContact->province_id != null && $modelContact->province_id == $key?' selected ':'' ?>><?= $val ?></option>
                    <?php endforeach; ?>
                </select>
    
                <?php
                $cityList = array();
                if ($modelContact->province_id) {
                    $cityList = ArrayHelper::map(City::find()->where(['province_id' =>$modelContact->province_id])->all(), 'id', 'name');
                }
                ?>
                <select id="community-city_id" class="form-control" name="UserContact[city_id]" style="width:180px;display: inline-block">
                    <option value="">请选择</option>
                    <?php foreach ($cityList as $key=>$val): ?>
                        <option value="<?= $key ?>"<?=$modelContact->city_id != null && $modelContact->city_id == $key?' selected ':'' ?>><?= $val ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>`
    
您需要登录后才可以评论。登录 | 立即注册