Home  >  Article  >  Web Front-end  >  Implement the method of adding and assigning tag sub-elements in jQuery

Implement the method of adding and assigning tag sub-elements in jQuery

亚连
亚连Original
2018-06-04 17:12:302755browse

Below I will share with you a jQuery method for adding and assigning tag sub-elements. It has a good reference value and I hope it will be helpful to everyone.

1. Define the 221f08282418e2996498697df914ce4e tag in the jsp page, as follows:

<p>
<span>科室:</span>
<select class="dept-name-show" style="width: 70%;">
</select>
</p>

2. Write js Statement:

<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>

3. Write the corresponding request statement:

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);
}
}

4. The implementation effect is as follows:

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to use ngrok express to solve WeChat interface debugging problems

How to use element-ui tables to make cells editable

How to clear the verification after closing the dialog in element ui

The above is the detailed content of Implement the method of adding and assigning tag sub-elements in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn