一、在jsp頁面中定義<select>標簽,如下:
<div><span>科室:</span><select class="dept-name-show" style="width: 70%;"></select></div>
二、編寫js語句:
<script>$(function () {var dname = $(".dept-name-show").eq(0);//選定<select>標簽var url = "${pageContext.request.contextPath}/getDepts.do";//請求路徑$(".dept-name-show").click(function () {$.get(url,function (res) {var len = res.length;var op = dname.children().length;if (op < len) {var pp = "<option></option>";for (var i = 0; i < len; i++) {dname.append(pp);dname.children().eq(i).text(res[i].name);}}})})})</script>三、編寫對應的請求語句:
List<Dept> deptList=null;@RequestMapping(value = "/getDepts",method = {RequestMethod.GET})public void getDepts(HttpServletResponse response) throws IOException {response.setCharacterEncoding("utf-8");response.setContentType("text/json;charset=utf-8");if (deptList == null){deptList = deptService.findAllDepts();}else {String res=JSON.toJSONString(deptList);response.getWriter().write(res);}}四、實現效果如下:


以上這篇jQuery實現標簽子元素的添加和賦值方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答