在JavaScript中从serializeArray省略空字符串 [英] Omit empty strings from serializeArray in javascript

查看:270
本文介绍了在JavaScript中从serializeArray省略空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于具有空字段的表单使用serializeArray()时,将为所有这些字段返回"".除了遍历数组并逐个删除它们之外,从数组中删除那些空字段的最佳方法是什么?

When using serializeArray() for a form with empty fields it returns "" for all those fields. What is the best way to omit those empty fields from the Array besides iterating over the Array and deleting them one by one?

推荐答案

由于serializeArray创建对象数组,因此必须进行迭代,并且必须检查每个值.
使用过滤器似乎很合适:

Iteration is neccessary as serializeArray creates an array of objects, and each value has to be checked.
Using a filter seems appropriate :

$('form').serializeArray().filter(function(k) {
    return $.trim(k.value) != "";
});

FIDDLE

这篇关于在JavaScript中从serializeArray省略空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆