首页 > 代码库 > jQuery Ajax遍历表格,填充数据,将表格中的数据一条一条拼成Jason数组

jQuery Ajax遍历表格,填充数据,将表格中的数据一条一条拼成Jason数组

  $.ajax({

                   url: baseURL + "InvoiceSale/OnQuotaInvoiceSale", //点击核销单号时,点击核销时,交互的页面

              type: "POST",
                 data: JSON.stringify(createTable.resultData()),     //JSON
传递整个表格数据,得到字符串数组
               dataType: "json",
              success: function (result) {
                                  Success(result)
                      },
              error: function () {
                     alert("error");
                  }
                });

//在初始加载时,遍历填充表格

loadData: function (data) {
var $table = $("#tableAgainDetail"),
$trFirst = $("#trDetailFirst");
$table.find("tr").each(function (i) {
if (i > 2) {
$(this).remove();
}
});
if (!!data) {
$.each(data, function (i, info) {
$tr = $trFirst.clone().attr("id", "").show();
$tr.children("td").eq(0).text("00");
//$tr.val(info.LaneID);
//$tr.children("td").eq(1).text(info.SquadNo);
$table.append($tr);
});
}
$("input[name=spaninvoiceCode]:visible").live("blur", function () {
var flags = false, $this = $(this);
if ($(this).val().length != 12) {
$(this).css("background", "#FDB7BF");
$(".tooltip_main").text("发票代码应为12位");
$("#tips").show().css("left", $this.parent()[0].offsetLeft).css("top", $this.parent()[0].offsetTop + 121);
flags = true;

}
if (flags) {
return false;
}
$(this).css("background", "#FFFFFF");
$("#tips").hide();
});

$("input[name=‘spanstartNo‘]:visible,input[name=‘spanendNoT‘]:visible").live("blur", function () {
var flags = false, $this = $(this);
if ($(this).val().length < 6 || $(this).val().length > 8) {
$(this).css("background", "#FDB7BF");
$(".tooltip_main").text("起\止号长度应为6-8位");
$("#tips").show().css("left", $this.parent()[0].offsetLeft).css("top", $this.parent()[0].offsetTop + 121);
flags = true;
}
if (flags) {
return false;
}
$(this).css("background", "#FFFFFF");
$("#tips").hide();
});

$("input[name=‘spanstartNo‘],input[name=‘spanendNoT‘],.can,.cuo").live(‘keyup‘, function () {
if (createTable.isNum($(this))) {
createTable.calTrCount($(this).parent().parent());
} else {
$("#spanMsg").text("请输入数字");
}
$("#spanMsg").text("");

});
},

 

//循环数据,得到JSON数组,拼成Jason数组,传个后台
resultData: function () {
var $tr = $("#tableAgainDetail").find("tr:visible"),
result = [],
trData = http://www.mamicode.com/{},
//OprID = [], DefinedOn = [], laneID = [],
spaninvoiceCode = [], spanstartNo = [], spanendNoT = [],
amount = [], can = [], cuo = [], actual = [];
/*获取数据后,循环向表格中添加数据*/
$.each($tr, function (i, info) {
var $this = $(this), $tds = $this.children("td");
if ($tds.length == 0) {
return true;
}
/*如果TD的长度==8,获取传来的收费员编号,以及日期时间*/
if ($tds.length == 8) {
trData = http://www.mamicode.com/{},laneID=[], spaninvoiceCode = [], spanstartNo = [], spanendNoT = >amount = [], can = [], cuo = [], actual = [];
trData.oprID = $("#txtOprID").val(); //收费员编号
trData.definedOn = $("#txtDefinedOn").val(); //日期时间 
trData.laneID = "0";
}
//money.push($this.find("select[name=‘money‘]").val()); //选择票面金额
spaninvoiceCode.push($this.find("input[name=‘spaninvoiceCode‘]").val()); //发票代码
spanstartNo.push($this.find("input[name=‘spanstartNo‘]").val()); //起号
spanendNoT.push($this.find("input[name=‘spanendNoT‘]").val()); //止号
amount.push($this.find(".amount").text()); //张数
can.push($this.find("input[class=‘can‘]").val()); //残票
cuo.push($this.find("input[class=‘cuo‘]").val()); //错票
actual.push($this.find(".actual").text()); //实际发出
//trData.money = money.join();

trData.spaninvoiceCode = spaninvoiceCode.join();
trData.spanstartNo = spanstartNo.join();
trData.spanendNoT = spanendNoT.join();
trData.amount = amount.join();
trData.can = can.join();
trData.cuo = cuo.join();
trData.actual = actual.join();
result.push(trData);

});
return result;
},