简体   繁体   English

如何在 CakePHP 中添加 onChange 事件以选择标记?

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

I am trying to use AJAX to populate some select boxes that were auto-generated using CakePHP's formHelper.我正在尝试使用 AJAX 填充一些使用 CakePHP 的 formHelper 自动生成的选择框。 How do I add the onChange event listener to the select box?如何将onChange事件侦听器添加到选择框?

我通过在window.onload()函数中使用addEventListener()函数动态添加事件侦听器来解决它。

 <?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 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'));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在哪里以及如何为rails 3.1 select tag onchange事件编写事件处理程序? - where and how do I code an event handler for a rails 3.1 select tag onchange event? 选择标记中的onchange事件! - onchange event in select tag! 如何添加OnChange事件以选择使用custom-form-elements.js设置样式的元素? - How do I add OnChange event to select element that is styled with custom-form-elements.js? 如何使用select标签绑定onchange事件 - How to bind onchange event with select tag 如何将 onchange js 事件添加到 Django 中的 Select 小部件? - How can i add a onchange js event to Select widget in Django? 如何从 reactjs 中 select 标签(主)的 onchange 事件更新另一个 select 标签(子)? - How can i update another select tag(sub) from an onchange event of select tag(main) in reactjs? 如何在ul标签元素中添加onchange事件? - how to add onchange event to ul tag elements? 如何添加onchange事件以选择标记并使select标签也有使用mithril.js的选项兄弟 - How to add an onchange event to select tag and have the select tag also have option siblings using mithril.js 选择表单上的 Cakephp onChange 事件(下拉菜单) - Cakephp onChange event on select form (dropdown) 如何通过onChange事件将对象值传递给select标签? - how to pass the the object values to the select tag with onChange event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM