javascript 函数在 cakephp 中不起作用

标签 javascript jquery cakephp

我在地区、州和城市进行了级联。我在 Controller 和 View 中添加了以下代码。但我的 Javascript 函数 onchange 无法在 View 表单中工作,请帮忙

Controller 代码:

<?php        
    class StatesController extends AppController {

        public function beforeFilter() {
            parent::beforeFilter();
            $zones = $this->{$this->modelClass}->Zone->find("list", array('conditions' => array('is_active' => 1)));
            $this->set(compact('zones'));

            //allow actions
            $this->Auth->allow(array('get_states'));
        }

        public function add() {
            parent::add();
            $this->loadModel('Zone');
            //Renders the common form for add and edit actions
            $this->render('form');
        }

         * Get States. list According to zone_id by ajax call
         */
        function get_states() {
            $this->autoRender = false;
            $zone_id = 0;
            if (!empty($_POST['zone'])) {
                $zone_id = $_POST['zone'];
            }
            $states = $this->State->find('list', array(
                'conditions' => array(
                    'zone_id' => $zone_id,
                    'is_active' => ACTIVE
                ),
                'order' => array('name' => 'asc')
                ));

            $states = array_flip($states);  
            echo json_encode($states);
        }

    }`
code for view
<code>
<aside class="body_rt"> 
    <!--Page head start-->
    <div class="page_head">
        <h1> <?php echo ucwords($model); ?> Manager</h1>
        <div class="back">
            <?php echo $this->Html->link($this->Html->image('images/back.png'), array(
                            'action' => 'index'), array('escape' => false, 'title' => "Back"));
            ?>
        </div>
    </div>
    <!--Page head end--> 
    <!--Message head start-->
    <?php if (($this->Session->check('Message.failure'))) { ?> 
    <section class="bolg_warp custom_message">
        <div class="sub_head">Message</div>
        <div class="flashMessage failure-flash">
                <?php echo $this->Session->flash('failure'); ?>
        </div>
    </section>
    <?php } ?>
    <!--Message head end-->
    <!--Blog warp start-->
    <section class="bolg_warp">
        <?php echo $this->Form->create($model, array('type' => 'file', 
                'inputDefaults' => array( 'label' => false, 'div' => false ))); ?>
        <div class="three_column">

            <ul>
                <li>
                    <?php echo $this->Form->hidden('id', array('class' => 'text_field')); ?>
                </li>
                <li>
                    <h2>Outlet Code:</h2>
                    <h3>
            <?php echo $this->Form->input('outlet_code', array('class' => 'text_field')); ?>
                    </h3>
                </li>
                 <li>
                    <h2>Name: <span class ="required">*</span></h2>
                    <h3>
            <?php echo $this->Form->input('name', array(
                                'class' => 'text_field')); ?>
                    </h3>
                </li>

                <li>
                    <h2>Zone :</h2>
                    <h3>
                        <?php echo $this->Form->input('zone_id', array(
                                'options' => $zones,
                                'label' => false,
                                'div' => false, 
                                'value' => '',
                                'class' => 'text_field short_field', 
                                'empty' => 'All'
                            )); 
                        ?>
                    </h3>
                </li>
                <li>
                    <h2>State :</h2>
                    <h3>
                        <?php echo $this->Form->input('state_id', array(
                                'options' => $states,
                                'label' => false,
                                'div' => false, 
                                'value' => '',
                                'class' => 'text_field short_field', 
                                'empty' => 'All'
                            )); 
                        ?>
                    </h3>
                </li>



                <li>
                    <h2>Status: </h2>
                    <h3>
                          <?php echo $this->Form->input("is_active", array("type" => "checkbox", 
                            "class" => "form-control checkbox")) ?>
                    </h3>
                </li>
            </ul>
        </div>
        <div class="centerbutton">
            <ul>
                <li>
                    <?php echo $this->Form->submit('images/save.png' , array('class'=>'ajax-submit')); ?>
                </li>
                <li>
                    <?php 
            echo $this->Html->link($this->Html->image('images/cancle.png'), array('controller' => $controller, 'action' => 'index'), 
                        array('escape' => false));
                    ?>
                </li>
            </ul>
        </div>
        <?php echo $this->Form->end(); ?>
    </section>
    <!--Blog warp end--> 
</aside>

<!--cascding -->
<script>
    $(function(){  
    var domain = "http://" + document.domain;

        $("select#CityZoneId").on('change', function () {
             var zone_id = $(this).val();
             if (zone_id != '')
             {
                 var url = domain + "/states/get_states";

                 $.post(url, {zone: zone_id}, function (data) {

                     var obj = $.parseJSON(data);
                     $("#CityStateId option").remove();
                     $("#CityStateId").append("<option value=''>All</option>")
                     $.each(obj, function (i, value) {
                         $("#CityStateId").append("<option value='" + value + "'>" + i + "</option>")
                     });
                 });
             }
         });
    });
</script>

请告诉我哪里做错了

最佳答案

您似乎没有调用与输入中相同的 id,请尝试将您的 jQuery 更改为:

$("#zone_id").on('更改', function () { //你的东西 }

关于javascript 函数在 cakephp 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40079770/

相关文章:

javascript - JQuery 中未检测到单选按钮更改

javascript - jQuery 快速查看效果 - 将事件绑定(bind)到一个元素

javascript - 如何在 ReactJS 中用 setInterval 做一个 map() 来模拟 linux 启动过程?

javascript - 为什么我需要在 HTML 中注释 &lt;script&gt; 标签?

javascript - 页面刷新时如何运行ajax脚本

jquery - 在文本区域中,将当前行的方向从 "rtl"更改为 "ltr"

javascript - JQuery 日期选择器向结束日期 minDate 添加 1 天

mysql - 在创建条目之前检查条目是否存在仍然会引发重复条目错误

php - jquery和cakephp中的分页和过滤问题

javascript - 如何使用 cakePHP 执行 ajax 请求?