ThinkPHP2.1模版使用筆記 – 模版嵌套/AJAX級聯

來源:互聯網
上載者:User
最近項目用PHP,不太熟,用架構開發,熟悉過程中,記錄下來,備查

  標籤庫select標籤使用   <tagLib name="html" /><html:select options="productCategory"
selected="prodCateId" first="選擇分類" name="prod_cate_id" id="prod_cate_id"
/> $Cate
= D('ProductCategory');$list
= $Cate->field('prod_cate_id,prod_cate_name')->findAll();foreach ($list as $vo){    $groupList[$vo['prod_cate_id']] = $vo['prod_cate_name'];}$this->assign('productCategory',$groupList);$this->assign('prodCateId','');$this->display('edit');   <volist>標籤的嵌套使用  <volist name="list" id="vo">

    {$vo.prod_cate_name}    <volist name="vo['voo']"
id="sub">        {$sub.prod_cate_name}    </volist>

</volist> $Dao
= M('ProductCategory');$list
= $Dao->where('ifnull(prod_cate_pid,0)=0')->order('prod_cate_order asc')->findAll();$Sub
= M('ProductCategory');foreach ($list as $n=>$val){    $list[$n]['voo']=$Sub->where('prod_cate_pid='.$val['prod_cate_id'])->select();}$this->assign('list',$list);

$this->display(); AJAX二級聯動的實現  <script type="text/javascript"
src="__ROOT__/public/scripts/jquery-1.7.1.min.js"></script> 

<script type="text/javascript">function ajaxSelect(url,objone,objtwo,defval){

    var $s1=$(objone);    var $s2=$(objtwo);    $s1.change(function(){        var curval = objone.options[objone.selectedIndex].value;        $s2.html("");        $("<option>").text("二級分類").val("").appendTo($s2);        $.ajax({               type: "POST",               url: url,               data: "catepid="+curval,             dataType: 'json',             success: function(obj){                if(obj.data)                {                    $(eval(obj.data)).each(function(i,elm){                        appendOptionTo($s2,elm.prod_cate_name,elm.prod_cate_id,defval);                    });                }             }         });    }).change();    function appendOptionTo(o,k,v,d){        var opt=$("<option>").text(k).val(v);        if(v==d){opt.attr("selected", "selected")}        opt.appendTo(o);    }}$(function(){    ajaxSelect('__URL__/changeCategory',$('#cate_one').get(0),$('#cate_two').get(0),'{$vo.cate_two}');


});

</script> <tagLib name="html" />

<html:select options="productCategory"
selected="prodCateId" first="選擇分類" name="cate_one" id="cate_one" /> 

<select name="cate_two" id="cate_two"><option value="">二級分類</option></select> 

public function
changeCategory(){    $id= $_POST['catepid'];    header("Content-type:text/html;
charset=utf-8");    $Cate
= D('ProductCategory');    $list
= $Cate->field('prod_cate_id,prod_cate_name')->where('prod_cate_pid='.$id)->select();    $this->ajaxReturn(json_encode($list),'','',0);

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.