jQuery ajax請求返回list數(shù)據(jù)動態(tài)生成input標(biāo)簽,并把list數(shù)據(jù)賦值到input標(biāo)簽

字號:


    這篇文章主要介紹了jQuery ajax請求返回list數(shù)據(jù)動態(tài)生成input標(biāo)簽,并把list數(shù)據(jù)賦值到input標(biāo)簽的相關(guān)資料,需要的朋友可以參考下
    直接給大家貼代碼了,具體內(nèi)容如下所示:
    // 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();
    }
    關(guān)于jQuery ajax請求返回list數(shù)據(jù)動態(tài)生成input標(biāo)簽,并把list數(shù)據(jù)賦值到input標(biāo)簽的全部介紹到此結(jié)束,希望對大家有所幫助!