Home >Web Front-end >JS Tutorial >Detailed explanation of the steps to add assignment to child elements using jQuery

Detailed explanation of the steps to add assignment to child elements using jQuery

php中世界最好的语言
php中世界最好的语言Original
2018-04-19 13:55:212346browse

This time I will bring you jQuery Detailed explanation of the steps for adding values ​​to sub-elements. What are the precautions for adding values ​​to sub-elements with jQuery? The following is a practical case. Let’s take a look. take a look.

##1. Define the

<p>
<span>科室:</span>
<select>
</select>
</p>

2. Write js statements:

<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>";
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);
}
}</dept>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

AngularJS implements the function of users changing passwords

JS implements infinite loading and paging function on the mobile terminal

Detailed explanation of the steps to add assignment to child elements using jQueryDetailed explanation of using v-cloak in Vue

The above is the detailed content of Detailed explanation of the steps to add assignment to child elements using 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