简体   繁体   English

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

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

i have two tables user and department where department has two fields id and name i want to create a view so that when someone selects a department name from the dropdownlist the user's name of all in that department show in another dropdownlist using AJAX and How to call that in controller 我有两个表用户和部门,部门有两个字段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>

this is the script i am using and in view the department dropdown is like this 这是我正在使用的脚本,并且在部门下拉列表中是这样的

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

Anyone please help me with this ajax and also the controller 任何人都可以帮助我使用这个ajax以及控制器

According to your code, can u try to replace 'id' => 'department' with 'id' => 'department_id' . 根据您的代码,您是否可以尝试将'id' => 'department'替换为'id' => 'department_id' Cause it's seen here you are using department_id as selector but your department_id id as not declared in dropdownlist. 因为在这里可以看到您正在使用department_id作为选择器,但是您的department_id ID未在dropdownlist中声明。 Here you declared department as ID. 在这里,您将部门声明为ID。 So selector is not found. 因此找不到选择器。 So Just replace 'id' => 'department' with ''id' => 'department_id'', Hope it can be helpful to you. 因此,只需将'id' => 'department'替换为'id' => 'department' ',希望对您有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM