ajax上传多行数据及数组循环添加对象

24 篇文章 1 订阅
	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()将对象转为字符串

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值