javascript - Remove empty values before form submit jquery - Stack Overflow

I have form with several text inputs. When user clicks submit on this form - text input field values be

I have form with several text inputs.

When user clicks submit on this form - text input field values being serialized and bee parameters of GET request like this

?param1+=26482&param2=

I need to check: if parameter has no value then remove it. So only parameters with values will be appended to GET request.

How it should be done?

My submit function

$("body").on('click','#form_submit_button', function(e){
    var form = $('#filter_form');
    change_url('?'+form.serialize());
});

function change_url(link)
{
    var IE = IE || 0;

    if (IE == 0) {
        var stateObj = { foo: "bar" };
        history.pushState(stateObj, "page 2", link);
    } else {
        location.href = link;
    }
}

I have form with several text inputs.

When user clicks submit on this form - text input field values being serialized and bee parameters of GET request like this

?param1+=26482&param2=

I need to check: if parameter has no value then remove it. So only parameters with values will be appended to GET request.

How it should be done?

My submit function

$("body").on('click','#form_submit_button', function(e){
    var form = $('#filter_form');
    change_url('?'+form.serialize());
});

function change_url(link)
{
    var IE = IE || 0;

    if (IE == 0) {
        var stateObj = { foo: "bar" };
        history.pushState(stateObj, "page 2", link);
    } else {
        location.href = link;
    }
}
Share Improve this question edited Oct 20, 2014 at 12:53 nikodz 7275 silver badges13 bronze badges asked Oct 20, 2014 at 12:37 micgeronimomicgeronimo 2,1495 gold badges24 silver badges45 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You can use the following js which filter your form including only inputs with a value.

http://jsfiddle/63ux5ap9/

  $(document).ready(function () {
        $('#filter_form').submit(function () {
            var text = $(this).find(":input").filter(function () {
                return $.trim(this.value).length > 0
            }).serialize();
            console.log('text: ', text);
            return false;
        });
    });

jQuery $.fn.serialize is a bination of $.param and $.fn.serializeArray methods:

jQuery.fn.serialize = function () {
    return jQuery.param(this.serializeArray());
}

It means that in order to achiever what you are after you can use the same approach and perform additional filtering of the resulting array returned by serializeArray:

var form = $('#filter_form'),
    params = $.param(form.serializeArray().filter(function(el) {
        return $.trim(el.value);
    }));

change_url('?' + params);

What do you need is:

var i = str.lastIndexOf("&");

So

if(str.length <= str.lastIndexOf("=")) {
   var i = str.lastIndexOf("&");
   str = str.substring(0, i); 
}

By the way, I would remove problem from radix!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745464117a4628839.html

相关推荐

  • javascript - Remove empty values before form submit jquery - Stack Overflow

    I have form with several text inputs. When user clicks submit on this form - text input field values be

    10小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信