這次帶給大家jQuery給子元素添加賦值步奏詳解,jQuery為子元素添加賦值的注意事項有哪些,下面就是實戰案例,一起來看一下。
一、在jsp頁面中定義
1 2 3 4 5 | <p>
<span>科室:</span>
<select>
</select>
</p>
|
登入後複製
二、寫js語句:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <script>
$( function () {
var dname = $( ".dept-name-show" ).eq(0);
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>
|
登入後複製
三、寫對應的請求語句:
1 2 3 4 5 6 7 8 9 10 11 12 | 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>
|
登入後複製
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
推薦閱讀:
AngularJS實作使用者修改密碼功能
JS在行動端實作無限載入分頁功能
Vue中v-cloak使用詳解
#
以上是jQuery為子元素添加賦值步奏詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!