我想在for循环中计算后返回函数结果

时间:2017-03-15 23:56:52

标签: javascript jquery ajax

我想在保存值之前检查是否是有效项目。然后我创建java脚本函数来检查验证和返回结果。但问题是这个函数在验证项之前返回,如果条件,则始终为true条件。我的代码如下。有人可以帮我吗? 这是一系列的ajax调用,我不知道如何使用回调...

if(IsValidItems() != ''){
//Do something
}

function IsValidItems() {

var IsvalidStatus = '';
var lineqty = 0;
var LineNumber = -1;

var allRowData = jQuery("#tblJQGrid").jqGrid("getRowData");

	for (var i = 0; i < allRowData.length - 1; i++) {
		if (allRowData[i].BulkItem != "False") {
			if (allRowData[i].quantity != '') {
				lineqty = parseInt(allRowData[i].quantity);
				LineNumber = i + 1;
				var postURL = "/BookingDetail/GetItemAvailablity?ItemCode=" + allRowData[i].itemCode + "&StartDate=" + allRowData[i].StartDate + "&EndDate=" + allRowData[i].EndDate + "&srno=" + allRowData[i].srno + "&locationID=" + allRowData[i].Location;                    
				$.ajax({
					url: postURL,
					dataType: "json",
					contentType: "application/json; charset=utf-8",
					data: "",
					type: "POST",
					async: true,
					dataFilter: function (data) {
						return data;
					},
					success: function (result) {
						if (lineqty > parseInt(result)) {
							IsvalidStatus = IsvalidStatus + "," + LineNumber;
						}
					},
					error: function (XMLHttpRequest, textStatus, errorThrown) { }
				});
			}
		}
	}
	return IsvalidStatus;
}
          

0 个答案:

没有答案