jquery 获取当前select onchange事件

简介: jquery 获取当前select onchange事件

jquery 获取当前select onchange事件


后台代码

    @GetMapping("/add")
    public String add(HttpServletRequest request, ModelMap mmap)
    {
        String longShortPathId = request.getParameter("longShortPathId");
        String subjectId = request.getParameter("subjectId");
        //当前路径对应考季id
        String seasonId = request.getParameter("seasonId");
        mmap.put("seasonId",seasonId);
        //获取科目下考季列表
        List<Map> seasons = baseService.getSeasonListBySubjectId(subjectId);
        mmap.put("seasons",seasons);
        //获取当前路径对应考季id下的课程列表
        List<Map> courses = new ArrayList<>();
        for (int i = 1; i < 10; i++) {
            Map map = new HashMap(2);
            map.put("courseId",i);
            map.put("courseName","课程"+i);
            courses.add(map);
        }
        mmap.put("courses",courses);
        return prefix + "/add";
    }

跳转到add.html页面


前端代码 add.html

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
  <th:block th:include="include :: header('新增长短路径拓展')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-longShortPathExtend-add">
      <input type="hidden" id="courseIds" name="courseIds"/>
      <input type="hidden" id="seasonIds" name="seasonIds"/>
      <div class="form-group">
        <label class="col-sm-3 control-label">选择课程:</label>
        <div class="col-sm-8" id="content">
          <div class="child">
            <div style="float: left;padding-left: 0px;" class="col-sm-6">
              <select name="seasonId" class="form-control m-b " onchange="refreshCourses(this);">
                <option th:each="season:${seasons}" th:selected="${season.seasonId} == ${seasonId}" th:value="${season.seasonId}"th:text="${season.seasonName}"></option>
              </select>
            </div>
            <div style="float: left;padding-left: 0px;" class="col-sm-6" >
              <select name="courseId" class="form-control m-b">
                <option th:each="course:${courses}" th:value="${course.courseId}"th:text="${course.courseName}"></option>
              </select>
            </div>
          </div>
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-3 control-label"></label>
        <div class="col-sm-8">
          <input onclick="addCourse();"  type="button" class="btn btn-w-m btn-primary" value="添加课程">
        </div>
      </div>
      <div id="courseCont" hidden>
        <div class="child">
          <div style="float: left;padding-left: 0px;" class="col-sm-6">
            <select name="seasonId" class="form-control m-b" onchange="refreshCourses(this)">
              <option th:each="season:${seasons}" th:selected="${season.seasonId} == ${seasonId}" th:value="${season.seasonId}"th:text="${season.seasonName}"></option>
            </select>
          </div>
          <div style="float: left;padding-left: 0px;" class="col-sm-6">
            <select name="courseId" class="form-control m-b">
              <option th:each="course:${courses}" th:value="${course.courseId}"th:text="${course.courseName}"></option>
            </select>
          </div>
        </div>
      </div>
    </form>
  </div>
    <div th:include="include::footer"></div>
    <script type="text/javascript">
    var prefix = ctx + "project/longShortPathExtend"
    $("#form-longShortPathExtend-add").validate({
      rules:{
        xxxx:{
          required:true,
        },
      },
      focusCleanup: true
    });
    function submitHandler() {
        debugger
        var season = "";
        var course = "";
            $(".child select[name='seasonId'] option:selected").each(function(){
                season += $(this).val() + ",";
            });
            $(".child select[name='courseId'] option:selected").each(function(){
                course += $(this).val() + ",";
            });
            $("#seasonIds").val(season);
            $("#courseIds").val(course);
          if ($.validate.form()) {
              //$.operate.save(prefix + "/add", $('#form-longShortPathExtend-add').serialize());
          }
      }
        //追加表格
        function addCourse() {
            var html = $("#courseCont").html();
            $("#content").append(html);
        }
        //考季变动时课程刷新
    function refreshCourses(element) {
      var seasonId = $(element).val();
        //console.log(element);
            $.ajax({
                type: "get",
                url: prefix+"/getCourseList",
                dataType: "html",
                data:{
                    seasonId: seasonId
                },
                async: false,
                success:function (html) {
                    if ($.trim(html) != "") {
                        $(element).parent().next().empty();
                        $(element).parent().next().append(html);
                    }
                }
            });
        }
  </script>
</body>
</html>


关注地方

image.png

image.png

附加返回html页面方法

    /**
     */
    @GetMapping("/getCourseList")
    public String getCourseList(HttpServletRequest request, ModelMap mmap)
    {
        String seasonId = request.getParameter("seasonId");
        List<Map> courses = new ArrayList<>();
        for (int i = 91; i < 100; i++) {
            Map map = new HashMap(2);
            map.put("courseId",i);
            map.put("courseName","课程"+i+"_"+seasonId);
            courses.add(map);
        }
        mmap.put("courses",courses);
        return prefix + "/course";
    }

对应页面

image.png


相关文章
N..
|
2月前
|
JavaScript 前端开发
jQuery事件处理
jQuery事件处理
N..
24 1
|
2月前
|
JavaScript
用JQuery实现选中select里面的option显示对应的div
用JQuery实现选中select里面的option显示对应的div
|
21小时前
|
JavaScript 前端开发
jQuery 事件
jQuery 事件
8 1
|
2天前
|
JavaScript 前端开发 开发者
jQuery中的ready()函数:优雅地处理页面加载事件
jQuery中的ready()函数:优雅地处理页面加载事件
3 0
|
2月前
|
JavaScript 前端开发 C++
【Web 前端】JavaScript window.onload 事件和 jQuery ready 函数有何不同?
【5月更文挑战第2天】【Web 前端】JavaScript window.onload 事件和 jQuery ready 函数有何不同?
|
2月前
|
Web App开发 JavaScript 前端开发
jquery的冒泡事件event.stopPropagation()
jquery的冒泡事件event.stopPropagation()
15 0
|
2月前
|
JavaScript 前端开发
jQuery中的事件与动画
jQuery中的事件与动画
|
2月前
|
JavaScript
jQuery动画与事件概念以及语法
jQuery动画与事件概念以及语法
|
10月前
|
JavaScript 前端开发 Java
JQuery的使用(工具方法、CSS属性及方法、筛选、文档处理、事件、动画)后续
元素 . animate({属性:属性值 } , time);缩放:width height移动:top left移动本元素,距离:top = "+=" left = "-="上一章:JQuery(入门~选择器)
|
9月前
|
JavaScript
jQuery04(事件&动画)
jQuery04(事件&动画)