开发者

How do I add an onChange event to select tag in CakePHP?

I am trying to use AJAX t开发者_如何学Co populate some select boxes that were auto-generated using CakePHP's formHelper. How do I add the onChange event listener to the select box?


I solved it by dynamically adding the event listener using the addEventListener() function inside a window.onload() function.


 <?php $html->script('jquery-1.4.2.min',false); ?>
<script type="text/javascript">
    $(document).ready(function(){
        $('#ProductBrandId').change(function(){
            getfamily()
        });
    })

    function getfamily(){
        $.ajax({
                type : "GET",
                url  : "getfamily?id="+$('#ProductBrandId').val()&rid='+Math.random()/9.9,
                success : function(opt){
                    $('#familybox').html(opt)
                }
            })
    }
</script>

admin_getfamily.ctp

<?php
    $options = array(
                 'type' => 'select',
                 'options' => $family,
                 'label' => '',
                 'div' => '',
                 'style' => 'width:235px',
                 'error'=> false,

            );

     echo $form->input(
             'Product.family_id',
             $options
    );
?>

in the controller

function admin_getFamily(){
        $this->layout = '';
        //debug($this->params['url']);
        $brand = $this->params['url']['id'];
        $family = array();
        if($brand > 0){
            $family = $this->Product->Brand->Family->find('list',array('fields' => 'Family.name','conditions' =>array('Family.brand_id' => $brand),'order' => 'Family.name'));
        }
        $this->set(compact('family'));
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜