jQuery ajax请求返回list数据动态生成input标签,并把list数据赋值到input标签
废话不多说了,直接给大家贴代码了,具体内容如下所示:
// js
<script type="text/javascript"> function myBtn_f() { var cnt = $('#myCnt').val(); syncAjax('myAjax.html', { 'cnt' : cnt }, function(result) { if (100 == result.statusCode) { var data = result.lst; var $d = $('#myDiv'); alert("[" + data + "]"); for ( var i = 0; i < data.length; i++) { var input1 = $("<input type='text' name='input1' />"); input1.attr('value', data[i]); $d.append(input1); } } else { alert("error"); } }); } function syncAjax(myUrl, myData, sufn) { $.ajax({ url : myUrl, data : myData, type : 'post', dataType : 'json', cache : false, async : false, success : function(result) { if (result.statusCode == 200) { alert("会话超时,请重新登录!"); window.location.href = "index.jsp"; } else { if (sufn) sufn(result); } }, error : function(msg) { alert("error:" + msg); } }); }; </script>
//html
<tr> <td><input type="text" id="myCnt" /></td> <td><div id="myDiv"></div></td> </tr> <input id="myBtn" type="button" value="create" onclick="myBtn_f()" />
// 后台
@RequestMapping("myAjax") @ResponseBody public String myAjax( @RequestParam(defaultValue = "0", required = false) int cnt) { List<String> lst = new ArrayList<String>(); for (int i = 0; i < cnt; i++) { lst.add("no:" + i); } JSONObject result = new JSONObject(); result.put("statusCode", 100); result.put("lst", lst); return result.toString(); }
关于jQuery ajax请求返回list数据动态生成input标签,并把list数据赋值到input标签的全部介绍到此结束,希望对大家有所帮助!
相关推荐
TONIYH 2020-06-11
开心就好 2020-06-10
donghongbz 2020-03-04
huha 2020-10-16
TLROJE 2020-10-26
echoes 2020-08-20
nercon 2020-08-01
zhanghaibing00 2020-06-28
Aveiox 2020-06-25
henryzhihua 2020-06-21
zhoutaifeng 2020-06-17
liangzhouqu 2020-06-16
x青年欢乐多 2020-06-06
KyrieHe 2020-06-03
bertzhang 2020-06-02
haokele 2020-05-29
niehanmin 2020-05-28