我有两个表user和department,其中department有两个字段id和name我想创建一个 View ,这样当有人从下拉列表中选择一个部门名称时,该部门中所有用户的名称都会使用AJAX显示在另一个下拉列表中,如何在 Controller 中调用它
<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 和 Controller
最佳答案
根据您的代码,您可以尝试将 'id' => 'department'
替换为 'id' => 'department_id'
。因为在这里可以看到您使用 department_id 作为选择器,但您的 department_id ID 未在下拉列表中声明。此处您将部门声明为 ID。所以找不到选择器。所以只需将 'id' => 'department'
替换为 ''id' => 'department_id'',希望对您有所帮助。
关于javascript - 想要从 cakephp 中部门下拉列表的 onchange 中选择用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32703229/