想要从更改cakephp部门的下拉列表中选择用户

want to select user from onchange of dropdown list of department in cakephp?

本文关键字:下拉列表 选择 用户 cakephp      更新时间:2023-09-26

我有两个表user和department其中department有两个字段id和name我想创建一个视图,以便当有人从下拉列表中选择一个部门名称时该部门中所有用户的名称显示在另一个下拉列表中使用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>

这是我正在使用的脚本department下拉列表如下

     <?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和控制器

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