javascript - remove parameters from request URL - Stack Overflow

I am submitting a large form. It has many fields with unspecified values, which translate to field=&

I am submitting a large form. It has many fields with unspecified values, which translate to field=&field2=&field3....

I don't want these in the GET url. Only keep fields with non-empty value.

I figured out a way to do it with jquery:

$('#form').live('submit', function() {
    $('input[value=""]', '#form').remove();
});

which I thought would do exactly what I wanted.

But apparently I am missing something, because this selects and removes inputs with entered text as well, not just the empty ones.

The above selector (before remove()) contains <input type=​"text" class=​"input-medium" name=​"field1" id=​"field1" value>​, so it looks as if there is no value set. There is a value set though, as confirmed by $('input[name="field1"]').val(), which correctly returns the text that is also visible on screen.

What's up with that? Any ideas?


Using jquery 1.7.2, Chrome 18.0.1025.168.

I am submitting a large form. It has many fields with unspecified values, which translate to field=&field2=&field3....

I don't want these in the GET url. Only keep fields with non-empty value.

I figured out a way to do it with jquery:

$('#form').live('submit', function() {
    $('input[value=""]', '#form').remove();
});

which I thought would do exactly what I wanted.

But apparently I am missing something, because this selects and removes inputs with entered text as well, not just the empty ones.

The above selector (before remove()) contains <input type=​"text" class=​"input-medium" name=​"field1" id=​"field1" value>​, so it looks as if there is no value set. There is a value set though, as confirmed by $('input[name="field1"]').val(), which correctly returns the text that is also visible on screen.

What's up with that? Any ideas?


Using jquery 1.7.2, Chrome 18.0.1025.168.

Share Improve this question asked May 5, 2012 at 13:32 user124114user124114 8,72113 gold badges47 silver badges69 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

Please use filtering of fields, since [value=""] selector checks the default states of the inputs.

$('#form').on('submit', function() {
    $(':input', this).filter(function() {
        return this.value.length == 0;
    }).remove();
});​

DEMO: http://jsfiddle/bCskH/

UPDATE: In order not removing input fields you can simply disable them:

$('#form').on('submit', function() {
    $(':input', this).filter(function() {
        return this.value.length == 0;
    }).prop('disabled', true);
});​

The effect will be the same, however IMHO much nicer :)

DEMO: http://jsfiddle/bCskH/1/

You can use jquery selector and serialize() to select only input that has value here is the example

<form id="test">
    <input name="field1" value="Test"><br/>
    <input name="field2" value=""><br/>
    <input name="field3" value="value"><br/>
    <input id="btn" type="button" value="submit"/>
</form>​


$('#btn').on('click',function() {
    alert($('#test input[value != ""]').serialize());
});​

also I doubt, .live() will work in jquery 1.7 use .on() instead. See this link and it says it has been deprecated

[value=".."] accesses the attribute which is not synced with the current value. Besides that, you cannot specify a selector as context - so move #form into the main selector and use .filter() to restrict the matched elements to empty ones:

$('#form input').filter(function() {
    return !$(this).val();
});

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

相关推荐

  • javascript - remove parameters from request URL - Stack Overflow

    I am submitting a large form. It has many fields with unspecified values, which translate to field=&

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信