3

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?

asked Jan 8, 2013 at 20:54
4
  • Change '&value' to '&myInputName' Commented Jan 8, 2013 at 20:56
  • The problem is that the function handles all of the checkboxes. They are dynamic in nature. I need to be able to tell which one was clicked/checked Commented Jan 8, 2013 at 20:59
  • You mean like var str = name + '=' + value? Commented Jan 8, 2013 at 20:59
  • Uh, yessir. :) Please post it and I'll credit you for the answer. Thank you! Commented Jan 8, 2013 at 21:02

3 Answers 3

2

Just use

var str = name + '=' + value;

What you have would output something like:

myInputName&value=myInputValue

Using name in that way is not useful.

answered Jan 8, 2013 at 21:03

Comments

2

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.

answered Jan 8, 2013 at 21:37

Comments

0

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/

answered Feb 11, 2016 at 16:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.