如何使用ajax请求bootstrap下拉框的内容

2025-10-20 18:54:18

1、在页面新增一个方法getDepts()

               function getDepts() {

                        $.ajax({

                                url:"${APP_PATH}/depts",

                                type:"get",

                                success:function(result) {

                                        console.log(result);

                                }

                        });

                }

如何使用ajax请求bootstrap下拉框的内容

2、创建一个业务逻辑层DepartmentService

package com.gwolf.crud.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.gwolf.crud.bean.Department;

import com.gwolf.crud.dao.DepartmentMapper;

@Service

public class DepartmentService {

        @Autowired

        private DepartmentMapper departmentMapper;

        public List<Department> getDepts() {

                return this.departmentMapper.selectByExample(null);

        }

}

如何使用ajax请求bootstrap下拉框的内容

3、新建一个DepartmentController控制类,用于部门信息的查询。

package com.gwolf.crud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.ResponseBody;

import com.gwolf.crud.bean.Department;

import com.gwolf.crud.bean.Msg;

import com.gwolf.crud.service.DepartmentService;

@Controller

public class DepartmentController {

        @Autowired

        private DepartmentService departmentService;

        

        @ResponseBody

        @RequestMapping("/depts")

        public Msg getDepts() {

                List<Department> list =   this.departmentService.getDepts();

                return Msg.success().add("depts", list);

        }

}

如何使用ajax请求bootstrap下拉框的内容

4、在浏览器控制台查看是否数据已经返回

如何使用ajax请求bootstrap下拉框的内容

5、显示部门部门信息在bootstrap模态对话框的下拉列表之中

function getDepts() {

                        $.ajax({

                                url:"${APP_PATH}/depts",

                                type:"get",

                                success:function(result) {

                                        $.each(result.extend.depts,function() {

                                                var optionEle = $("<option></option>").append(this.deptName).attr("value",this.deptId);

                                                optionEle.appendTo($("#dept_select"));

                                        });

                                }

                        });

                }

如何使用ajax请求bootstrap下拉框的内容

6、在浏览器中查看模态对话框的下拉列表数据是否成功。

如何使用ajax请求bootstrap下拉框的内容

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢