Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

jquery - Omit empty strings from serializeArray in javascript

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...