在项目中,对于数据的传输一般需要非空的判断,而数据字段较多时一般直接将表单序列化,此时如何判断非空,如下
因为将表单序列化时,数据格式为
trainKind=1&trainKindCode=1&trainTypeCode=1&selfWeight=1&weight=1&volume=1&loadPrice=1&length=1&width=1&hight=11&remark=1
所以可以将字段与对应的值分隔开,循环判断
var data = $('#addTypeForm').serialize();
var msg = '';
console.log(data);
var array = data.split("&");
for (var i = 0; i < array.length; i++) {
var kwarr = array[i].split("=");
console.log(kwarr);
for (var j = 0; j < kwarr.length; j++) {
if (kwarr[j] == null || kwarr[j] == '') {
$("#msgs").html('请把必填信息填上');
$("#promptIdModals").modal();
$('#showMask', window.parent.document).show();
return false;
}
}
}
其中的console.log(kwarr)格式为:
(2) ["trainKind", "1"]0: "trainKind"1: "1"length: 2__proto__: Array(0)
list.do:57 (2) ["trainKindCode", "1"]
list.do:57 (2) ["trainTypeCode", "1"]
list.do:57 (2) ["selfWeight", "1"]
list.do:57 (2) ["weight", "1"]
list.do:57 (2) ["volume", "1"]
list.do:57 (2) ["loadPrice", "1"]
list.do:57 (2) ["length", "1"]
list.do:57 (2) ["width", "1"]
list.do:57 (2) ["hight", "11"]
list.do:57 (2) ["remark", "1"]