select下拉框的数据回显

需求描述:select框,下拉后又很多的选项,选择一个,根绝后台代码做查询,完成之后,页面上的select框还是之前选的那个值

解决思路:select本质就是 value和text一一对应,根据你的select下拉菜单,可以在代码中看看value具体是什么,text是什么,比如说value是对应的id而text是对应的name,然后后台根绝选择的内容查询的时候,可以把这个id取出来,然后返回到select存在的页面,通过封装好的函数,来回显。话不多说贴代码。

代码:

<select id="select1" name="select1" class="form-control js-example-basic-single">
<option th:each="s : ${selectMap}" th:value="${s.key}" th:text="${s.value}" xmlns:th="http://www.w3.org/1999/xhtml"></option> //我的selectMap的key value 分别是id 和 name
</select>
<input type="hidden" name="dId" id="dId" th:value="${dId}"/>   //后台传过来的id  modelMap.addAttribute("dId",id); 

js代码:

<script type="text/javascript" th:inline="javascript" xmlns:th="http://www.w3.org/1999/xhtml">
var dId = [[${dId}]]; //后台传过来的id modelMap.addAttribute("dId",id);
if (CommnUtil.notNull(dId)) {
$("#select1").val(dId).trigger("change");//把select的value是id值的那个对应的文本显示出来 trigger是jQuery封装好的方法
} else {
$("#select1").val(null).trigger("change");//id为空的话 select框就是空
}
</script>

总结:对于这种小的知识点就是只能这样总结,因为找起来实在是太浪费时间了,身为菜鸟的我要加油!!!


原文地址:https://www.cnblogs.com/xuchao0506/p/9566216.html