1

Hi There I need to get the names and values of checkboxes that have been checked into an array name selected, I cannot for life of me get it working, below is my attempt, if someone could clrify what I am doing wrong that would be brilliant.

//Location AJAX
    //var dataObject = new Object();
    var selected = new Array();
    $('#areas input.radio').change(function(){ // will trigger when the checked status changes
        var checked = $(this).attr("checked"); // will return "checked" or false I think.
        // Do whatever request you like with the checked status
        if(checked == true) {
        /*$("input:checked").each(function() {
                selected.push($(this).attr('name')+"="+$(this).val();
            });
                alert(selected)*/
                getQuery = $(this).attr('name')+"="+$(this).val()+"&location_submit=Next";
                $.ajax({
                    type:"POST",
                    url:"/search/location",
                    data: getQuery,
                    success:function(data){
                        alert(getQuery);
                        console.log(data);
                        //  $('body.secEmp').html(data);
                    }
                });
        } else {
            //do something to remove the content here
            alert("Remove");
        }
    });
1
  • 2
    Do you use Firbug? If so, do get any error messages? If you do not use it, use it! :) Commented Apr 12, 2010 at 14:24

1 Answer 1

3

You're missing the closing parantheses on the call to selected.push. It should be:

selected.push($(this).attr('name')+"="+$(this).val());

Otherwise, it looks fine.

Another, possibly simpler, way to do the same thing is to use map instead of each:

var selected = $('input:checked').map(function() {
    return $(this).attr('name')+"="+$(this).val();
}).get();
Sign up to request clarification or add additional context in comments.

5 Comments

I noticed that right at the moment of posting, I notice this gives a comma seperated array, I am sending this array as a query string is there a way for me replace the comma with a &
@sea_1987: Use selected.join('&')
@interjay: selected.join('&') shows no change, it is still comma seperated
join works for me. Note that it won't change the value of selected: you need to use the value that selected.join('&') returns.
how should I be using, I dont really follow, I doing that method and then alerting selected and still seeing commas.

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.