My AJAX only fires once,

I have some ajax that is triggered when a checkbox is clicked, it essentially sends a query string to the PHP script and then returns the appropriate HTML, however, if I select a selection, it works fine if I then select another checkbox as well as the previous one. i dont get any activity that way ever, not even any errors in firebug, very curious if anyone has any ideas?

//Location AJAX
//var dataObject = new Object();
var selected = new Array();
//alert(selected);
$('#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) {
        //selected.join('&');
        selected = $('input:checked').map(function() {
            return $(this).attr('name')+"="+$(this).val();
        }).get();

            getQuery = selected.join('&')+"&location_submit=Next";
            alert(getQuery);
            $.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($(this).attr('name'));
    }
});

      

+2


source to share


2 answers


I figured out that since my ajax is loading on the new page with success, the actions were not placed on the elements as they only loaded once on the DOM, I moved the entire script into a function and called that on DOM Ready now and works fine.



0


source


I see you are using the variable check = $ (this) .attr ("checked"); I think this might be a problem because checked is a standard JS attribute, native JS. You can compare how to properly validate element on element and see if it is true or false. I would like to start by changing the name of the variable and move from there.

Another thing that can happen is losing a listener that could be called by the variable you selected. You don't need to declare your selections outside of your listener. Just declare it inside when you install it.



And if THAT doesn't help, providing some markup will help debug this issue, because there seems to be a lot going on here.

Good luck.

+3


source







All Articles