mercredi 22 février 2017

Getting value from checkbox created from $.post results

I have a set of checkboxes that are created from a $.post return result. These checkboxes are created each time a different person is selected from a drop down list, displaying different people for each.

$.post("api.php/atherapists", {clientID: clientID}).done(function(data){

  var data = JSON.parse(data);

  var trHTML = '';

  $.each(data, function(i, item){
      therapistID = item.therapist_id;
      therapistName = item.therapist_name;

      trHTML += '<tr><td><input type="checkbox" name="achkbox" id="achkbox" value="' + therapistID + '"></td><td>' + therapistName + '</td></tr>';
  });
  $('#tableAvailable').empty();
  $('#tableAvailable').append(trHTML);

});

My question is regarding getting the value from the checkbox when it is clicked. I plan on having a function fire off a mysqli query from a $.post based off when one of the checkboxes is clicked and sending its value over to php to be processed. Everytime i try to alert the value of the checked checkbox, nothing happens. I've tried each of these

$("input[name='achkbox']").change(function(){
    alert($(this).attr('value'));       
});

$('#achkbox').click(function(){
    alert($(this).attr('value'));        
});

$('#achkbox').change(function(){
    var id = $(this).val();
    if(id != null){
        alert(id);
    }
});

Nothing seems to return/display the value i need to send to php to process the person. I can figure out how to send the data over to php on the fly, i just can't figure out what I'm doing wrong with obtaining the value.




Aucun commentaire:

Enregistrer un commentaire