<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form id="editForm" action="">
<input name="userName" type="text" value="Kitty">
<input name="userAge" type="text" value="12">
<input name="userSchool" type="text">
</form>
<script type="text/javascript" src="jquery-1.12.3.js"></script>
<script type="text/javascript">
var ret ={};
$(':input', $('#editForm')).each(function() {
var nm = $(this).attr('name');
var vl = $(this).val();
if (isNotEmpty(nm) && isNotEmpty(vl)) {
ret[nm] = vl;
}
});
console.log("ret",ret)
function isEmpty(obj) {
try{
if(obj == null || obj == undefined) {
return true;
}
//判断数字是否是NaN
if(typeof obj === "number") {
if(isNaN(obj)) {
return true;
} else {
return false;
}
}
//判断参数是否是布尔、函数、日期、正则,是则返回false
if(typeof obj === "boolean" || typeof obj === "function" || obj instanceof Date || obj instanceof RegExp) {
return false;
}
//判断参数是否是字符串,去空,如果长度为0则返回true
if(typeof obj === "string") {
if(obj.trim().length == 0) {
return true;
} else {
return false;
}
}
if(typeof obj === 'object') {
//判断参数是否是数组,数组为空则返回true
if(obj instanceof Array) {
if(obj.length == 0) {
return true;
} else {
return false;
}
}
//判断参数是否是对象,判断是否是空对象,是则返回true
if(obj instanceof Object) {
//判断对象属性个数
if(Object.getOwnPropertyNames(obj).length == 0) {
return true;
} else {
return false;
}
}
}
} catch(e) {
console.log(e);
return false;
}
}
function isNotEmpty(obj) {
try{
if(obj == null || obj == undefined) {
return false;
}
//判断数字是否是NaN
if(typeof obj === "number") {
if(isNaN(obj)) {
return false;
} else {
return true;
}
}
//判断参数是否是布尔、函数、日期、正则,是则返回true
if(typeof obj === "boolean" || typeof obj === "function" || obj instanceof Date || obj instanceof RegExp) {
return true;
}
//判断参数是否是字符串,去空,如果长度为0则返回false
if(typeof obj === "string") {
if(obj.trim().length == 0) {
return false;
} else {
return true;
}
}
if(typeof obj === 'object') {
//判断参数是否是数组,数组为空则返回false
if(obj instanceof Array) {
if(obj.length == 0) {
return false;
} else {
return true;
}
}
//判断参数是否是对象,判断是否是空对象,是则返回false
if(obj instanceof Object) {
//判断对象属性个数
if(Object.getOwnPropertyNames(obj).length == 0) {
return false;
} else {
return true;
}
}
}
} catch(e) {
console.log(e);
return false;
}
}
</script>
</body>
</html>