dimanche 27 septembre 2015

How can I loop through all checked checkboxes in jQuery, getting their value in each loop?

I have this HTML/Spacebars in a Meteor template:

<label for="seljoblocs">Select the Jobs/Locations for the selected Worker</label>
<div id="seljoblocs" name="seljoblocs">
  {{#each jobLocs}}
    <input type="checkbox" value={{jl_jobloc}}><label>{{jl_jobloc}}</label>
  {{/each}}
</div>

This generates a checkbox for each "jobloc" via the Helper function:

jobLocs: function() {
  return JobLocations.find({}, {
    sort: {
      jl_jobloc: 1
    },
    fields: {
      jl_jobloc: 1
    }
  });
}

I have this jQuery for that template's form submit event:

'submit form': function(event, template) {
    event.preventDefault();
    var workerid = template.find('#selworker').value;

    Meteor.call('insertWorkerJobLocLookup', workerid, jobloc, function(err)          
    {
      if (err) {
        Session.set("lastErrMsg", err.message);
      } else {
        console.log(workerid + ' for ' + jobloc + ' inserted');
      } // else
    }); // Meteor call

    $('#selworker').val("");
    // TODO: Uncheck all the checkboxes
}

I need to loop through all the checked checkboxes, calling the 'insertWorkerJobLocLookup' method once for each checked checkbox, something like this (pseudocode):

'submit form': function(event, template) {
    event.preventDefault();
    var workerid = template.find('#selworker').value;

    foreach (checked checkbox czech) {
        var jobLocation = czech.value;
        Meteor.call('insertWorkerJobLocLookup', workerid, jobLocation, 
function(err) {
          if (err) {
            Session.set("lastErrMsg", err.message);
          } else {
            console.log(workerid + ' for ' + jobloc + ' inserted');
          } // else
        }); // Meteor call           
    }
    $('#selworker').val("");
    // TODO: Uncheck all the checkboxes
}

This portion:

foreach (checked checkbox czech) {
    var jobLocation = czech.value;

...of the pseudocode is where I have no clue what to do to first loop through the checked checkboxes, and then get the value.




Aucun commentaire:

Enregistrer un commentaire