2

I've got some REALLY simple fields, like a checkbox for yes/no's and some text inputs for width/height etc.

I'd like to have users check a box for a feature and define their sizes, so if they check <input type="checkbox" id="header" name="header" value="true" /> then I want to add &show_header=true to the query string.

I've got a very basic fiddle set up, but I can't seem to get it to work properly. Like, if you check the box, then fill in the value, but uncheck the box, it starts to repeat things and I end up with &show_header=true&show_header=true&show_header=true&show_header=true&show_header=true

2
  • 2
    just give the input the name show_header and use serialize()... api.jquery.com/serialize Commented Nov 30, 2012 at 22:48
  • Thanks for the link - I was unaware of that! :) Commented Nov 30, 2012 at 22:53

3 Answers 3

2

You may simply use serialize() method:

function displayVals() {
    $("code").html(function(i, val) {
        var query = $("input").serialize();
        return "http://...?method=review_box&amp;" + query;
    });
}

DEMO: http://jsfiddle.net/AYQyB/4/

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you're trying to reinvent jQuery's .serialize() function.

<form>
    <div><input type="text" name="a" value="1" id="a" /></div>
    <div><input type="text" name="b" value="2" id="b" /></div>
    <div><input type="hidden" name="c" value="3" id="c" /></div>
    <div>
        <textarea name="d" rows="8" cols="40">4</textarea>
    </div>
    <div><select name="e">
        <option value="5" selected="selected">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
    </select></div>
    <div>
        <input type="checkbox" name="f" value="8" id="f" />
    </div>
    <div>
        <input type="submit" name="g" value="Submit" id="g" />
    </div>
</form>

jQuery:

$('form').submit(function() {
    alert($(this).serialize());
    return false;
});

Produces:

a=1&b=2&c=3&d=4&e=5

Comments

0

Maybe so?

<input class="my-vars" type="checkbox" name="rbshowheader" id="rbshowheader" value="true" />
<input class="my-vars" type="text" name="rbwidth" id="rbwidth_id" value="" />
<pre><code>http://yoursite.whirlocal.com?method=review_box</code><span id="query"></span></pre>

and JavaScript

$(function() {
  $('.my-vars').change( function() {
     var sQuery = $('.my-vars').serialize();
     $('#query').html('&'+sQuery);
   });
});

Comments

Your Answer

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.