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

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

Jun 04, 2018 pm 05:12 PM
jquery element Add to

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 <select> tag in the jsp page, as follows:

&lt;p&gt;
&lt;span&gt;科室:&lt;/span&gt;
&lt;select class=&quot;dept-name-show&quot; style=&quot;width: 70%;&quot;&gt;
&lt;/select&gt;
&lt;/p&gt;
Copy after login

2. Write js Statement:

&lt;script&gt;
$(function () {
var dname = $(&quot;.dept-name-show&quot;).eq(0);//选定&lt;select&gt;标签
var url = &quot;${pageContext.request.contextPath}/getDepts.do&quot;;//请求路径
$(&quot;.dept-name-show&quot;).click(function () {
$.get(
url,
function (res) {
var len = res.length;
var op = dname.children().length;
if (op &lt; len) {
var pp = &quot;&lt;option&gt;&lt;/option&gt;&quot;;
for (var i = 0; i &lt; len; i++) {
dname.append(pp);
dname.children().eq(i).text(res[i].name);
}
}
}
)
})
})
&lt;/script&gt;
Copy after login

3. Write the corresponding request statement:

List&lt;Dept&gt; deptList=null;
@RequestMapping(value = &quot;/getDepts&quot;,method = {RequestMethod.GET})
public void getDepts(HttpServletResponse response) throws IOException {
response.setCharacterEncoding(&quot;utf-8&quot;);
response.setContentType(&quot;text/json;charset=utf-8&quot;);
if (deptList == null){
deptList = deptService.findAllDepts();
}else {
String res=JSON.toJSONString(deptList);
response.getWriter().write(res);
}
}
Copy after login

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 of this Website
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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add a TV to Mijia How to add a TV to Mijia Mar 25, 2024 pm 05:00 PM

How to add a TV to Mijia

Outlook stuck on adding account [Fixed] Outlook stuck on adding account [Fixed] Mar 23, 2024 pm 12:21 PM

Outlook stuck on adding account [Fixed]

How to add a new script in Tampermonkey-How to delete a script in Tampermonkey How to add a new script in Tampermonkey-How to delete a script in Tampermonkey Mar 18, 2024 pm 12:10 PM

How to add a new script in Tampermonkey-How to delete a script in Tampermonkey

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery?

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery?

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

jQuery Tips: Quickly modify the text of all a tags on the page

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Use jQuery to modify the text content of all a tags

How to add MySQL root password? How to add MySQL root password? Mar 14, 2024 pm 05:12 PM

How to add MySQL root password?

See all articles