1

I have several inputs in a form, but what I want to achieve is to send only the ones who have at least 1 character (alphanumeric), but for those who have empty or whitespace values must not be sent. The problem is that when a user sends a whitespace by mistake by pressing the spacebar it serializes a plus sign (+).

So far this is what I do to send serialized non-empty values.

//this will print a query string with the values, but for whitespaces it sends '+' signs.
$('#myform').find('input').not('[value=""]').serialize(); 
4

3 回答 3

2

You can do following:

var obj = {};
$('#myform').find('input').each(function() {

    var value = $(this).val().trim();
    var inputName = $(this).attr('name');

    if(value.length != 0) {
        obj[inputName] = value;
    }
});

$(obj).serialize();
于 2013-07-02T16:37:29.390 回答
2

You can use $.trim:

$('#myform').find('input').filter(function() {
     return $.trim(this.value) != "";
}).serialize();

This will also take the actual user input (.value property) not the .defaultValue (value attribute) like .not('[value=""]')

于 2013-07-02T16:41:30.007 回答
1

By Googling this seems to work pretty fine:

$('#myform').find('input:filled').serialize(),

reference: http://jqueryvalidation.org/filled-selector/

于 2013-07-02T16:45:02.187 回答