yii2中在view页面写js代码/城市联动

来源:互联网 发布:srt字幕制作软件 编辑:程序博客网 时间:2024/04/28 10:20

1.

<script><?php $this->beginBlock('js_end') ?> 

$(document).on('change','.address',function(){

       var area_id = $(this).val();

       var _this = $(this);

       _this.nextAll().remove();

       if(area_id==0){

           return false;

       }

       var url = 'index.php?r=test/area_child';

       $.getJSON(url,{'area_id':area_id},function(msg){

           if(msg.length>0){

                var str="<selectclass='address'><option>..请选择</option>";

               $(msg).each(function(k,v){

                    str+="<optionvalue="+v.area_id+">"+v.area_name+"</option>";

                })

               str+="</select>";

                _this.after(str);

           }

       })

   })

<?php $this->endBlock() ?>

</script>

<?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END); ?>




2.

<script src='css/jq.js'></script>
<script>
    $(document).on('change','.address',function(){
    var area_id = $(this).val();
    var _this = $(this);
    _this.nextAll().remove();
    if(area_id==0){
        return false;
    }
    var url = 'index.php?r=test/area_child';
    $.getJSON(url,{'area_id':area_id},function(msg){
        if(msg.length>0){
            var str = '';
            str+="<select class='address'><option>..请选择</option>";
            $(msg).each(function(k,v){
                str+="<option value="+v.area_id+">"+v.area_name+"</option>";
            })
            str+="</select>";
            _this.after(str);
        }
    })
})
</script>

0 0
原创粉丝点击