首頁 web前端 js教程 jQuery ajax請求回傳list資料動態產生input標籤,並把list資料賦值到input標籤_jquery

jQuery ajax請求回傳list資料動態產生input標籤,並把list資料賦值到input標籤_jquery

May 16, 2016 pm 03:07 PM

廢話不多說了,直接給大家貼程式碼了,具體內容如下:

// js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

<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

1

2

3

4

5

<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()" />

登入後複製

// 後台

1

2

3

4

5

6

7

8

9

10

11

12

13

@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標籤的全部介紹到此結束,希望對大家有所幫助!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

在JavaScript中替換字符串字符 在JavaScript中替換字符串字符 Mar 11, 2025 am 12:07 AM

在JavaScript中替換字符串字符

自定義Google搜索API設置教程 自定義Google搜索API設置教程 Mar 04, 2025 am 01:06 AM

自定義Google搜索API設置教程

示例顏色json文件 示例顏色json文件 Mar 03, 2025 am 12:35 AM

示例顏色json文件

構建您自己的Ajax Web應用程序 構建您自己的Ajax Web應用程序 Mar 09, 2025 am 12:11 AM

構建您自己的Ajax Web應用程序

8令人驚嘆的jQuery頁面佈局插件 8令人驚嘆的jQuery頁面佈局插件 Mar 06, 2025 am 12:48 AM

8令人驚嘆的jQuery頁面佈局插件

什麼是這個'在JavaScript? 什麼是這個'在JavaScript? Mar 04, 2025 am 01:15 AM

什麼是這個'在JavaScript?

通過來源查看器提高您的jQuery知識 通過來源查看器提高您的jQuery知識 Mar 05, 2025 am 12:54 AM

通過來源查看器提高您的jQuery知識

10張移動秘籍用於移動開發 10張移動秘籍用於移動開發 Mar 05, 2025 am 12:43 AM

10張移動秘籍用於移動開發

See all articles