admin管理员组文章数量:1422488
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 input
s 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 input
s 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 03 Answers
Reset to default 3Please 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();
});
本文标签: javascriptremove parameters from request URLStack Overflow
版权声明:本文标题:javascript - remove parameters from request URL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745371069a2655732.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论