views:

93

answers:

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");
        }
    });
+2  A: 

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();
interjay
sea_1987
interjay
sea_1987
interjay
how should I be using, I dont really follow, I doing that method and then alerting selected and still seeing commas.
sea_1987
`alert(selected.join('`
interjay
brilliant thankyou!
sea_1987