"Uncaught TypeError: Cannot use 'in' operator to search for 'length' in" when getting checked values by data attribute
"Uncaught TypeError: Cannot use 'in' operator to search for 'length' in" when getting checked values by data attribute
这是复选框组的视图
这是我的html上面视图的代码(还有更多复选框,这里只提到2个):
<div class="panel-body subject_names">
<div class="col-md-12">
<label for="checkboxes-0" class="col-md-7">
KE1-Financial Accounting & Reporting Fundamen
</label>
<input type="checkbox" data-subject-name="KE1-Financial Accounting & Reporting Fundamen" data-subject-ai="925" class="big-checkbox col-md-1 subject1" name="exam_subjects" id="checkboxes-0" value="1012">
<div class="col-md-4 pull-left">
<input id="training_institute" name="textinput" type="text" placeholder="" class="input-md">
</div>
</div>
<div class="col-md-12">
<label for="checkboxes-0" class="col-md-7">
KE2-Management Accounting Information
</label>
<input type="checkbox" data-subject-name="KE2-Management Accounting Information" data-subject-ai="926" class="big-checkbox col-md-1 subject1" name="exam_subjects" id="checkboxes-0" value="1013">
<div class="col-md-4 pull-left">
<input id="training_institute" name="textinput" type="text" placeholder="" class="input-md">
</div>
</div>
</div>
这是我的 js 代码:
var subjects_names = [];
$.each($("input[name='exam_subjects']:checked").attr("data-subject-name"), function() {
subjects_names.push($(this).val());
});
异常:
Exam_application_c:775 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in KE1-Financial Accounting & Reporting Fundamen
您在 string
上使用 $.each
,它必须是 object
或 array
。
尝试将您的代码更改为此
var subjects_names = [];
$.each($("input[name='exam_subjects']:checked"), function () {
subjects_names.push($(this).data("subjectName"));
});
或者更好的是使用 .each
作为对 jQuery 对象的迭代
var subjects_names = [];
$("input[name='exam_subjects']:checked").each(function () {
subjects_names.push($(this).data("subjectName"));
});
这是复选框组的视图
这是我的html上面视图的代码(还有更多复选框,这里只提到2个):
<div class="panel-body subject_names">
<div class="col-md-12">
<label for="checkboxes-0" class="col-md-7">
KE1-Financial Accounting & Reporting Fundamen
</label>
<input type="checkbox" data-subject-name="KE1-Financial Accounting & Reporting Fundamen" data-subject-ai="925" class="big-checkbox col-md-1 subject1" name="exam_subjects" id="checkboxes-0" value="1012">
<div class="col-md-4 pull-left">
<input id="training_institute" name="textinput" type="text" placeholder="" class="input-md">
</div>
</div>
<div class="col-md-12">
<label for="checkboxes-0" class="col-md-7">
KE2-Management Accounting Information
</label>
<input type="checkbox" data-subject-name="KE2-Management Accounting Information" data-subject-ai="926" class="big-checkbox col-md-1 subject1" name="exam_subjects" id="checkboxes-0" value="1013">
<div class="col-md-4 pull-left">
<input id="training_institute" name="textinput" type="text" placeholder="" class="input-md">
</div>
</div>
</div>
这是我的 js 代码:
var subjects_names = [];
$.each($("input[name='exam_subjects']:checked").attr("data-subject-name"), function() {
subjects_names.push($(this).val());
});
异常:
Exam_application_c:775 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in KE1-Financial Accounting & Reporting Fundamen
您在 string
上使用 $.each
,它必须是 object
或 array
。
尝试将您的代码更改为此
var subjects_names = [];
$.each($("input[name='exam_subjects']:checked"), function () {
subjects_names.push($(this).data("subjectName"));
});
或者更好的是使用 .each
作为对 jQuery 对象的迭代
var subjects_names = [];
$("input[name='exam_subjects']:checked").each(function () {
subjects_names.push($(this).data("subjectName"));
});