admin管理员组文章数量:1433132
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¶m2=
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¶m2=
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
3 Answers
Reset to default 4You 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!
本文标签: javascriptRemove empty values before form submit jqueryStack Overflow
版权声明:本文标题:javascript - Remove empty values before form submit jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745464116a2659455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论