jeudi 5 mai 2016

jQuery checking checkboxes context

My application presents data in rows with several text input fields and a checkbox. When the user updates one of the fields, an ajax update of the database is kicked off. The values of each field are taken from the row and all used in a common update function. The issue is successfully interrogating the state of the checkbox in the same row when it's not the field that changed.

I'm guessing from the behaviour that my query isn't actually returning a checkbox, but can't figure what it should be.

Within the function, going via the row I could obtain the text input field values, but the checkbox only when it's the field that changed. I have sidestepped the issue by naming the checkbox and querying directly on name, but I would love to know why the initial navigation approach was unsuccessful:

// change function assigned in document ready
$("input.multiple, input.price, tr.linkItem input:checkbox").change(function(event){
    changeMatch ($(this));
});

.....

function changeMatch(ipObj) { // get values from row and submit
  // get plain text from a cell - works
  var linkId = $(ipObj).closest('tr').children('td:nth-child(1)').text();
  // get value of a text input field - works
  var itemQty = $(ipObj).closest('tr').children('td:nth-child(3)').text();
  // try to get state of checkbox - never true
  if ( $(ipObj).closest('tr').children('input:checkbox').prop('checked')) {
      var pigs = 'fly';
  }
  // try to get state of checkbox - works if it was the checkbox that changed
  if ( $(ipObj).closest('tr').children('input:checkbox').context.checked ) {

Any pointers appreciated.

Aucun commentaire:

Enregistrer un commentaire