如何使用Ajax和php填充可靠的下拉列表 [英] How to populate dependable drop-down using Ajax and php

查看:93
本文介绍了如何使用Ajax和php填充可靠的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Ajax在下拉菜单上管理数据.

Hi i want to manage data on drop-down menu using Ajax.

数据库字段:

1.id

2.name

3.部门

myDesgin.php

     <select id="id"></select>
     <select id="name"></select>
     <select id="department"></select>

1.如果我选​​择了一个下拉菜单,想要更改另一个下拉菜单,则取决于使用Ajax选择的值.

1.If i selected one drop-down menu want to change another drop-downs depend on selected value using Ajax.

2.是否有可用的代码,如果我选择一个下拉菜单,它将转到另一个子窗口,并使用 Ajax 以表格格式(如报告)显示数据.

2.Is there any code available, if i select one drop-down it go to another child window and display data as in table format(like report) using Ajax.

预先感谢.

请给我示例代码,因为我是ajax的初学者,如果有人提供带有代码的解释(对于ajax),则非常欢迎.

Please give me example code, because i am beginner to ajax , most welcome if someone provide explanation with code(for ajax).

推荐答案

是的,请检查以下jquery ajax代码. 在此示例中,如果更改部门",则它将填充名称"下拉列表.

Yes, check following jquery ajax code. In this example, if you change "Department" then it will populate the listing of "Name" dropdown.

$(document).on("change", '#department', function(e) {
            var department = $(this).val();
            

            $.ajax({
                type: "POST",
                data: {department: department},
                url: 'admin/users/get_name_list.php',
                dataType: 'json',
                success: function(json) {

                    var $el = $("#name");
                    $el.empty(); // remove old options
                    $el.append($("<option></option>")
                            .attr("value", '').text('Please Select'));
                    $.each(json, function(value, key) {
                        $el.append($("<option></option>")
                                .attr("value", value).text(key));
                    });														
	                

                    
                    
                }
            });

        });

这篇关于如何使用Ajax和php填充可靠的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆