I have the following code that builds a query string:
var name = $(this).next()[0].name;
var value = $(this).next()[0].checked;
var str = name+'&value='+value;
and outputs an array on my server like this:
[value] => false
What I would like to have is something more like this:
array(
[myInputName] => value
)
How can I achieve this?
3 Answers 3
Just use
var str = name + '=' + value;
What you have would output something like:
myInputName&value=myInputValue
Using name
in that way is not useful.
Comments
In the future, take a look at jQuery.param to build query strings. For example:
var p = {};
p[name] = value;
var str = $.param(p)
Although it's overkill for your simple example, it'll help you in the future.
Comments
try $('yourselection').serialize()
This will produce a querystring for you from the inputs those have names.
https://api.jquery.com/serialize/
And check param too. It works nice for custom parameters. http://api.jquery.com/jquery.param/
'&value'
to'&myInputName'
var str = name + '=' + value
?