【问题标题】:want to select user from onchange of dropdown list of department in cakephp?想从 cakephp 中部门下拉列表的 onchange 中选择用户?
【发布时间】:2015-09-21 19:39:11
【问题描述】:

我有两个表用户和部门,其中部门有两个字段 id 和名称我想创建一个视图,以便当有人从下拉列表中选择部门名称时,该部门中所有用户的名称显示在另一个下拉列表中使用 AJAX 和如何在控制器中调用它

 <script>
jQuery(document).ready(function ($) {
    //jQuery('#searchTable').dataTable();
    $('#department_id').change(function () {
        jQuery('#user').empty();
        var data2 = {};
        data2['department_id'] = jQuery(this).val();
        var json = JSON.stringify(data2);
        jQuery.ajax({
            type: "POST",
            url: "/AjaxRequests/name",
            data: json,
            dataType: "json",
            success: function (response) {
                var app = "<option value>All</option>";
                jQuery('#user').append(app);

                jQuery.each(response, function (i, text) {
                    jQuery('#user').append(jQuery('<option></option>').val(i).html(text));
                });

            }
        });
    });
   </script>

这是我正在使用的脚本 鉴于部门下拉菜单是这样的

     <?php echo $this->Form->input('department_id', array('onChange' => 'showFields(this.value)', 'class' => 'form-control-custom', 'id' => 'department_id', 'type' => 'select', 'label' => true, 'label' => 'department:', 'options' => $departments, 'empty' => 'Select A Department', 'required' => 'false'))
    ?>

请任何人帮助我处理这个 ajax 和控制器

【问题讨论】:

    标签: javascript jquery ajax cakephp cakephp-2.0


    【解决方案1】:

    根据您的代码,您可以尝试将 'id' =&gt; 'department' 替换为 'id' =&gt; 'department_id' 吗?因为在这里可以看到您使用 department_id 作为选择器,但您的 department_id id 未在下拉列表中声明。在这里,您将 department 声明为 ID。所以没有找到选择器。所以只需将'id' =&gt; 'department' 替换为''id' => 'department_id'',希望对您有所帮助。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 2015-11-27
    • 2011-08-15
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多