1. 程式人生 > >ajax上傳多行資料

ajax上傳多行資料

	function pay(collectingCompanyId){
		var aCount=$("#actualCount").text();
		var aMoney=$("#actualMoney").text();
		var html = "<div style='padding:10px;'>摘要:      <input type='text' id='title' name='title' /><br>"
		
			+"實結數量:<input type='text' id='aCount' name='aCount' readonly='readonly' value="+aCount+" /><br>"
			+"實付金額:<input type='text' id='aMoney' name='aMoney' readonly='readonly' value="+aMoney+" /><br>"
			+"備註:      <input type='text' id='remark' name='remark' /><br></div>";
			var rkDetailArray = new Array();
			$('input:checkbox[name=checkbox]:checked').each(function(){
				//console.log($(this).val());
				var obj = new Object();
				obj.storageInBillDetailId=$(this).val();
				rkDetailArray.push(obj);
			});
			console.log("rkDetailArray:"+rkDetailArray);
			if(rkDetailArray==''){
				$.jBox.tip("請選擇入庫單!");
			}else{
				$.jBox(html, { title: "付款", submit: submit,buttons: { '確定': true, '取消': false}  });
			}
		var submit = function (v, h, f) {
			
			 if(v==true){
				$.ajax({
					url:"${ctx}/xldx/accountPayment/saveRecord",
					type:"post",
					dataType:"text",
					data:{"title" : f.title,"actulCount":f.aCount,"count":$('#countSum').text(),"payment":$('#priceSum').text(),"actualPayment":f.aMoney,"rkDetailArray":JSON.stringify(rkDetailArray),"collectingCompanyId":collectingCompanyId},
					success:function(data){
						console.log("data:"+data);
						if(data == "success"){
							alertx("提交成功");
							window.location.href = "${ctx}/xldx/accountDetails/paymentBalanceTableDetailIsComplete?collectingCompanyId="+collectingCompanyId;
						}
					},
					error : function(data) {
						console.log("dataerror:"+data);
						layer.closeAll();
						alertx("出錯了");
					}
				});
			} 
		    return true;
		};

		
	}
如果是上傳多行資料,需要建立陣列
var rkDetailArray = new Array();

然後在each迴圈中每次新增一個數據,並push到陣列中

var obj = new Object();
obj.storageInBillDetailId=$(this).val();
rkDetailArray.push(obj);    

最後ajax上傳鍵值對資料(data),

"rkDetailArray":JSON.stringify(rkDetailArray)

注:使用JSON.stringify()將物件轉為字串

如果上傳的不是多行資料,則不用建立陣列,例子如下

function submitBill() {
		//登入使用者的單位 啟用狀態
		var useable="${merchant.useable}";
		if(useable !="1"){
			$(".cd-popup-z-cw").html("你的單位 已被停用");
			$(".cd-popup-z").addClass('is-visible');
			return false;
		}
		var isBlock="${merchant.isBlock}";
		if(isBlock =="1"){
			$(".cd-popup-z-cw").html("您所屬單位的賬戶已凍結");
			$(".cd-popup-z").addClass('is-visible');
			return false;
		}
		var billInfo = new Object();
		var receiverWay = $("#receiveWay").text();			//判斷自取還是物流
		var receiverName = $("#fullName").text();			//獲取收貨人名字
		var receiverPhone = $("#telephone").text();			//獲取收貨人電話電話
		var receiverAddress = $("#storeAndmember").text();	//獲取收貨人地址
		
		if(receiverWay == "物流"){
			if (receiverAddress == null || receiverAddress.trim() == ""){
				$(".cd-popup-z-cw").html("請填寫收貨地址");
				$(".cd-popup-z").addClass('is-visible');
				return;
			}
		}
		if (receiverName == null || receiverName.trim() == ""){
			$(".cd-popup-z-cw").html("請填寫收貨人");
			$(".cd-popup-z").addClass('is-visible');
			return;
		}
		if (receiverPhone == null || receiverPhone.trim() == ""){
			$(".cd-popup-z-cw").html("請填寫收貨電話");
			$(".cd-popup-z").addClass('is-visible');
			return;
		}
	
		/*病人資訊*/
		var orderId = "${orderId}";
		var hospitalId = new Array();   				//醫院id
		var patientCode = new Array();					//病人住院號
		var patientName = new Array();					//病人名稱
		var patientSex = new Array();					//病人性別
		var patientStature = new Array();				//病人身高
		var patientAge = new Array();					//病人年齡
		
		if (orderId != "") {							//如果是銷售單取出器械的下單
			hospitalId = "${orderInfo.hospitalId}";
			patientCode = "${orderInfo.patientCode}";
			patientName = "${orderInfo.patientName}";
			patientAge = "${orderInfo.patientAge}";
			patientSex = "${orderInfo.patientSex}";
			patientStature = "${orderInfo.patientStature}";
			patientPhone = "${orderInfo.patientPhone}";
			patientWeight = "${orderInfo.patientWeight}";
		} else {										//手機端第一次下單
			hospitalId = $(".hospital-ul").find("li").eq(0).find("p").eq(1).find("input").val();
			patientCode = $("#patientCode").val().trim();
			patientName = $("#patientName").val().trim();
			patientAge = $("#patientAge").val().trim();
			patientSex = $("#patientSex").text().trim();
			patientStature = $("#patientStature").val().trim();
			patientPhone = $("#patientPhone").val().trim();
			patientWeight = $("#patientWeight").val().trim();
			if(patientStature == "cm"){
				patientStature = "";
			}
			if(patientWeight == "kg"){
				patientWeight = "";
			}
		}
		/* if(patientName == "" || patientCode == ""){
			$(".cd-popup-z-cw").html("請填寫完整病人資訊");
			$(".cd-popup-z").addClass('is-visible');
			return;
		}
		var pw = /^1[34578]\d{9}$/;  
		if(patientPhone != ""){
			if(!pw.test (patientPhone)){                 //判斷手機號格式
	           	$(".cd-popup-z-cw").html("請填寫正確手機號碼");
				$(".cd-popup-z").addClass('is-visible');
				return;
			}
		} */
		
		/*處理手術時間  */
		var picktime = $("#picktime").text();
		var operationData = selectOperatorYear + '-' + selectOperatorMonth + '-' + picktime.substring(0, 2) + ' ' + '23' + ':' + '59';

		/*處理取貨時間  */
		var picktimeDay = $("#picktime3").text();
		var picktimeHours = $("#picktime4").text();
		var picktimeMin = $("#picktime5").text();
		var pickUpData = selectTakeYear + '-' + selectTakeMonth + '-' + picktimeDay.substring(0, 2) + ' ' + picktimeHours.substring(0, 2) + ':' + picktimeMin.substring(0, 2);
		//計算兩個時間的間隔
		var operationTime = Date.parse(new Date(operationData));
    	var pickUpTime = Date.parse(new Date(pickUpData));
		var dateInterval = Math.abs(parseInt((operationTime - pickUpTime)/1000/3600/24));
		if((operationTime-pickUpTime) <= 0){
			$(".cd-popup-z-cw").html("到貨時間不符不能超過手術日期");
			$(".cd-popup-z").addClass('is-visible');
			return;
		}
		/* if(dateInterval > 2){
			$(".cd-popup-z-cw").html("手術日期跟到貨時間最多隻能間隔兩天");
			$(".cd-popup-z").addClass('is-visible');
			return;
		} */
		var leaveMessage = $("#leaveMessage").val();
		if(leaveMessage.length > 50){
			$(".cd-popup-z-cw").html("留言不得超過50字");
			$(".cd-popup-z").addClass('is-visible');
			return;
		}
        if (hospitalId == undefined || hospitalId == null || hospitalId == "") {
			$(".cd-popup-z-cw").html("請選擇手術醫院");
			$(".cd-popup-z").addClass('is-visible');
			return false;
		}
		//取貨方式 取貨人
		billInfo.receiverWay = receiverWay;			//自取還是物流
		billInfo.receiverName = receiverName;		//收貨人名稱
		billInfo.receiverPhone = receiverPhone;		//收貨人電話
		billInfo.receiverAddress = receiverAddress;	//收貨人地址
		//病人資訊
		billInfo.hospitalId = hospitalId;			//醫院id
		billInfo.operationData = operationData;		//手術日期
		billInfo.pickUpData = pickUpData;			//取貨時間或者發貨時間
		billInfo.leaveMessage = leaveMessage;
		billInfo.patientCode = patientCode;
		billInfo.patientName = patientName;
		billInfo.patientSex = patientSex;
		billInfo.patientAge = patientAge;
		billInfo.patientStature = patientStature;
		billInfo.patientPhone = patientPhone;
		billInfo.patientWeight = patientWeight;
		
		billInfo.needReturn = $("#orderWay").text()=='購買'?'0':'1';	//下單方式
		//下單產品
		//billInfo.productId = "${productId}";
		billInfo.storageId = "${storageId}";				//倉庫id
		billInfo.storageName = "${storageName}";			//倉庫名稱
		billInfo.senderName = "${storageManagerName}";		//庫管名稱
		billInfo.senderPhone = "${storageManagerPhone}";	//庫管電話
		billInfo.senderAddr = "${storageAddr}";				//倉庫地址
		
		billInfo.productParams = '${productParams}';									//主產品陣列
		billInfo.productModelParams = '${productModelParams}';							//主產品規格陣列
		billInfo.productGroupParams = '${productGroupParams}';							//配套產品陣列
		billInfo.instrumentGroupParams = '${instrumentGroupParams}';					//配套器械陣列
		billInfo.baseInstrumentToolParams = '${baseInstrumentToolParams}';				//基礎器械陣列
		billInfo.baseInstrumentToolModelParams = '${baseInstrumentToolModelParams}';	//基礎器械規格陣列
		billInfo.cartInfo = '${cartInfo}';
		billInfo.orderId = orderId;

 		$.ajax({
			url : "${ctx}/order/submitConfirmOrder",
			async : true,
			type : "POST",
			dataType : "text",
			data : {
				billInfo : JSON.stringify(billInfo)
			},
			beforeSend : function(i) { //觸發ajax請求開始時執行
				$(".confirm-box").css("display", "block");
			},
			success : function(data) {
				var obj = JSON.parse(data);
				if(obj.status=="1"){
					$(".confirm-box").css("display", "none");
					$(".cd-popup-z-cw").html(obj.massage);
					$(".cd-popup-z").addClass('is-visible');
				}else{
					window.location.href = "${ctx}/order/toOrderUnfinishList";
				}
			}
		});
	}
使用JSON.stringify()將物件轉為字串