Home >Backend Development >PHP Tutorial >javascript - 关于thinkphp 与 ajax 实现二级联动下拉列表

javascript - 关于thinkphp 与 ajax 实现二级联动下拉列表

WBOY
WBOYOriginal
2016-06-06 20:14:041260browse

表的结构
cId 分类id
cName 分类名称
gid 父级id
这是我的HTML页面

<code>               <volist name="rs" id="val">
                   <option value="{$val.cId}">{$val.cName}</option>                      
                   </volist>
           
           <select name="lable" id="lables">
                               
           </select></code>

ajax代码
<script><br>$('#gType').click(function(){</script>

<code>$(this).change(function(){
    var objectModel = {};
    var   value = $(this).val();
   var   type = $(this).attr('id');
    objectModel[gType] =value;
    $.ajax({
        cache:false,
        type:"POST",
        url:"__APP__/Goods/goodsAdd",
        dataType:"json",
        data:objectModel,
        timeout:30000,
        success:function(data){
            $("#lables").empty();
            var count = data.length;
            var i = 0;
            var b="";
               for(i=0;i<count b value='"+data[i].cId+"'>"+data[i].cName+"";
               }
            $("#lables").append(b);
        }
    });
   });</count></code>

}
);


thinkphp接收页面
public function goodsAdd(){

<code>    $model=M("category");        
    $rs=$model->where("pid=0")->select();
    $this->assign("rs",$rs);
    $this->display();
    $result = array();
    $cate =$_POST['gType'];
    var_dump($cate);
    $result = M('category')->where(array('pid'=> $cate))->field('cId,cName')->select();
    var_dump($result);
    $this->ajaxReturn($result,"JSON");
    
    
}</code>

请问哪里出错了 ajax刚接触

回复内容:

表的结构
cId 分类id
cName 分类名称
gid 父级id
这是我的HTML页面

<code>               <volist name="rs" id="val">
                   <option value="{$val.cId}">{$val.cName}</option>                      
                   </volist>
           
           <select name="lable" id="lables">
                               
           </select></code>

ajax代码
<script><br>$('#gType').click(function(){</script>

<code>$(this).change(function(){
    var objectModel = {};
    var   value = $(this).val();
   var   type = $(this).attr('id');
    objectModel[gType] =value;
    $.ajax({
        cache:false,
        type:"POST",
        url:"__APP__/Goods/goodsAdd",
        dataType:"json",
        data:objectModel,
        timeout:30000,
        success:function(data){
            $("#lables").empty();
            var count = data.length;
            var i = 0;
            var b="";
               for(i=0;i<count b value='"+data[i].cId+"'>"+data[i].cName+"";
               }
            $("#lables").append(b);
        }
    });
   });</count></code>

}
);


thinkphp接收页面
public function goodsAdd(){

<code>    $model=M("category");        
    $rs=$model->where("pid=0")->select();
    $this->assign("rs",$rs);
    $this->display();
    $result = array();
    $cate =$_POST['gType'];
    var_dump($cate);
    $result = M('category')->where(array('pid'=> $cate))->field('cId,cName')->select();
    var_dump($result);
    $this->ajaxReturn($result,"JSON");
    
    
}</code>

请问哪里出错了 ajax刚接触

PHP代码段你原来正常的是怎样的?怎么又有display又有ajaxReturn的。。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn