$("#approve").on("click", function () { var data = [];//聲明一個數組 var t = $(".table tbody tr").length-2; $(".table tbody tr:lt("+t+")").each(function () { //獲取行號,從第t行開始遍歷 if ($(this).find("td:eq(0) input").prop("checked") == true) { //只遍歷checkbox選中的 var param = {};//聲明一個json param.id = $(this).find("td:eq(0) input").val();//開始json的鍵值對賦值 var extractAmount = $(this).find("td:eq(4)").text(); extractAmount = $.trim(extractAmount); param.extractAmount = extractAmount; var uid = $(this).find("td:eq(7)").text(); uid = $.trim(uid); param.uid = uid; data.push(param);//將封裝好的json添加到數組 } }); console.log(data); var error = checkElement(data); if (error) { return; } $.ajax({ url: "${pageContext.request.contextPath }/activityCommission/approveCommission.do", type: "POST", dataType: 'json', cache: false, data: {param: JSON.stringify(data)},//將json轉換成string類型,提交給後臺 success: function (data) { if (data.success) { toastr.success("審覈成功!"); setTimeout(function () { window.location.reload(); }, 500); } else { toastr.error(data.resultMsg); } } }); function checkElement(data) { if (data.length < 1) { toastr.warning("至少選擇一項"); } };});