【问题标题】:CodeIgniter - Why JQuery won't update a form_dropdownCodeIgniter - 为什么 JQuery 不会更新 form_dropdown
【发布时间】:2014-09-19 23:21:09
【问题描述】:

我有两个下拉菜单 - 组和子组。如果组已更改,那么我想用新组的子项的新列表更新子组。

这是我的下拉菜单代码:

<div  id="inp_p_group"class="control-group">
  <label class="control-label">Select Group:</label>
   <div class="controls">
    <?php
    $p_groups = array();
    $p_groups['-1'] = 'no selection';
    foreach ($groups->result() as $g_row){
        $p_groups[$g_row->tbl_group_id] = $g_row->vch_group_name;
    }
    echo form_dropdown('p_groups', $p_groups, $row->fk_group_id, 'style="width: 140px; font-size: 13px" id="p_groups"'); 
    ?>
   </div>
 </div>       

 <div  id="inp_p_groupsub"class="control-group">
   <label class="control-label">Select Sub Group:</label>
   <div class="controls">
   <?php
     $p_groupsub = array();
     $p_groupsub['-1'] = 'no selection';
     foreach ($sub_groups->result() as $s_row){
        $p_groupsub[$s_row->tbl_group_sub_id] = $s_row->vch_sub_name;
     }
     echo form_dropdown('p_groupsub', $p_groupsub, $row->fk_group_sub_id, 'style="width: 140px; font-size: 13px" id="p_groupsub"'); 
   ?>
   </div>
 </div>       

这是我的 JScript 代码

$('#p_groups').change(function() {
    var select = document.getElementById("p_groups");
    var myvalue= $.trim(select.options[select.selectedIndex].innerHTML);
    // now populate the Sub Group selector with the children of the selected Group
    group_id = select.options[select.selectedIndex].value;
    // make call to function to get the sub-group data
    $.post(
        "../product_controller/get_subgroup_json",// function call
        {'gid' : group_id},                         // function data
        function (retval) {                         // callback function
            //var sub_list = document.getElementById("p_groupsub");
            var sub_list = $('#p_groupsub')[0];
            sub_list.options.length = 0; // this removes existing options
            $.each(retval, function(index, element) {
                // create an Option object
                var opt = document.createElement("option");
                // Add an Option object to Drop Down/List Box
                sub_list.options.add(opt);
                // Assign text and value to Option object
                opt.value = index;
                opt.text = element;
            });
        }, // end callback
        "json"
        ); // end post
}); // end p_groups-change function

我一生都无法弄清楚为什么这不起作用,非常感谢任何帮助。

G.

更新 我一直在进一步研究这一点,我在网站的另一部分使用相同的 JS 来做同样的事情。在这两个地方我都调用函数

 "../product_controller/get_subgroup_json"

返回子组列表。

我从正在工作的代码中调用它,POST 标头看起来像这样

 ../index.php/product_controller/get_subgroup_json?gid=12

但是当我从不起作用的代码中调用它时,POST 看起来像:

../index.php/product_controller/product_controller/get_subgroup_json?gid=12

这两种情况下的 JS 是相同的,所以我不知道额外的 'product_controller/' 来自哪里。

G.

【问题讨论】:

  • 把你的php代码结果(html)和js放在jsfiddle.net
  • 代码使用Bootstrap 2.0。这会在jsfiddle中工作吗?我没有使用它的经验。
  • 好的,我想我已经在 jsfiddle 中工作了 - 这里:jsfiddle.net/unplugngo/ab9gqxLt
  • 不要把你的php放在jsfiddle中……把你的php创建的html放到浏览器上(查看源代码在你的浏览器上)
  • 好的,谢谢。我已经做到了。

标签: jquery codeigniter


【解决方案1】:

这里的问题在于对获取子组下拉列表数据的函数的调用,如下所示:

 $.post(
    "../product_controller/get_subgroup_json",  // function call

因为视图是从 product_controller 调用的,所以这个路由隐含在任何进一步的函数调用中,所以调用应该是:

 $.post(
    "../get_subgroup_json",  // function call

我认为 URI 没有在某处导致错误真的很奇怪,所以如果有人对此事有任何想法给我一些反馈,我将不胜感激。

再次感谢您的关注。

U.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    相关资源
    最近更新 更多