js發送post請求獲取json遍歷寫入html

不怎麼用, 記錄下方便之後用。數據基本上都在後臺處理完了,拿過來直接用javascript

function ajax_post(url) {
        $('#html').empty();

		var id = $('#game_id option:selected').val();
        var postData = {
            'id': id,
        };

        $.post(url,postData,function(requst){
            if(requst.code == 1){
                var data = JSON.parse(requst.data);
                var len = getJsonLength(data);//獲取json數組長度
                var htmlSpan = "";
                for (var i = 0; i < len; i++ ){
                    htmlSpan += "<label><input type='checkbox' value='" + data[i]['id'] + "' name='type[]'>" + data[i]['name'] + "</label>";
				}
				$('#html').html(htmlSpan);
            }else {
                alert(requst.msg);
            }
        });
    }
    //獲取json數組長度
    function getJsonLength(jsonData){
        var jsonLength = 0;
        for(var item in jsonData){
            jsonLength++;
        }
        return jsonLength;
    }
相關文章
相關標籤/搜索