我在 Meteor 模板中有这个 HTML/空格键:
<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>
这会通过 Helper 函数为每个“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());
});
关于jquery - 如何循环遍历 jQuery 中所有选中的复选框,并在每个循环中获取它们的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32811635/