如何遍历 jQuery 中所有选中的复选框,在每个循环中获取它们的值?
How can I loop through all checked checkboxes in jQuery, getting their value in each loop?
我在 Meteor 模板中有这个 HTML/Spacebars:
<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>
这会通过辅助函数为每个 "jobloc" 生成一个复选框:
jobLocs: function() {
return JobLocations.find({}, {
sort: {
jl_jobloc: 1
},
fields: {
jl_jobloc: 1
}
});
}
我有这个 jQuery 用于该模板的表单提交事件:
'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
}
我需要遍历所有选中的复选框,为每个选中的复选框调用一次 'insertWorkerJobLocLookup' 方法,就像这样(伪代码):
'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
}
这部分:
foreach (checked checkbox czech) {
var jobLocation = czech.value;
...伪代码是我不知道该怎么做才能首先遍历选中的复选框,然后获取值。
$('input[type=checkbox]:checked').each(function(index){
//part where the magic happens
console.log(index+' checkbox has value' +$(this).val());
});
辛迪斯的想法奏效了;这有效:
$('input[type=checkbox]:checked').each(function(index) {
var workerid = template.find('#selworker').value;
var jobloc = $(this).val();
Meteor.call('insertWorkerJobLocLookup', workerid, jobloc, function(err) {
if (err) {
Session.set("lastErrMsg", err.message);
} else {
console.log(workerid + ' for ' + jobloc + ' inserted');
} // else
}); // Meteor call
});
我在 Meteor 模板中有这个 HTML/Spacebars:
<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>
这会通过辅助函数为每个 "jobloc" 生成一个复选框:
jobLocs: function() {
return JobLocations.find({}, {
sort: {
jl_jobloc: 1
},
fields: {
jl_jobloc: 1
}
});
}
我有这个 jQuery 用于该模板的表单提交事件:
'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
}
我需要遍历所有选中的复选框,为每个选中的复选框调用一次 'insertWorkerJobLocLookup' 方法,就像这样(伪代码):
'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
}
这部分:
foreach (checked checkbox czech) {
var jobLocation = czech.value;
...伪代码是我不知道该怎么做才能首先遍历选中的复选框,然后获取值。
$('input[type=checkbox]:checked').each(function(index){
//part where the magic happens
console.log(index+' checkbox has value' +$(this).val());
});
辛迪斯的想法奏效了;这有效:
$('input[type=checkbox]:checked').each(function(index) {
var workerid = template.find('#selworker').value;
var jobloc = $(this).val();
Meteor.call('insertWorkerJobLocLookup', workerid, jobloc, function(err) {
if (err) {
Session.set("lastErrMsg", err.message);
} else {
console.log(workerid + ' for ' + jobloc + ' inserted');
} // else
}); // Meteor call
});