多条件查询 应对 url 无用 null 值现象 处理
例如:
http://xxoo.b2b.com/orders?city_id=5&repertory_id=7&area_id=39&order_status=&order_type=&pay_type=&id=&add_user_id=&client_id=&client_name=&tel=&address=&startTime=&endTime=
优化后:
http://xxoo.b2b.com/orders?city_id=5&repertory_id=7&area_id=39
优化代码:
<script>
$(function () {
// 提交按钮单击事件
$('#btn').click(function () {
// 读取form表单中的所有数据列表
var d = {};
var t = $('form').serializeArray();
$.each(t, function () {
d[this.name] = this.value;
});
// 转换为json字符串
var str = JSON.stringify(d);
var jsonString = str;
//转换为json对象
var jsObject = JSON.parse(jsonString);
// 判断空值
for (var arr in jsObject) {
if (jsObject[arr] == '') {
delete jsObject[arr];
}
}
// 去除无用的属性
delete jsObject['_token'];
delete jsObject['_method'];
// 拼接参数
var search = [];
for (var Key in jsObject) {
search += '&' + Key + '=' + jsObject[Key];
}
var str = '?' + search.substr(1);
// 拼接网址
var path = window.location.pathname;
var aaa = path + str;
// 跳转
window.location.href = aaa;
// 阻止提交按钮默认事件
return false;
});
});
</script>
网友评论