![](/img/trans.png)
[英]select all checkbox checked all checkboxes have image in the label are not checked
[英]select all checkbox and have the value of checked checkbox to 'on' jquery
问题是if ($("input[name='resolution[]']:checked")) {
, $("input[name='resolution[]']:checked")
将返回一个jQuery对象,其中包含所有选定的具有名称resolution[]
复选框
你需要
$('#res').click(function () {
var resolutiontemp = {};
var resolution = [];
$("input[name='resolution[]']").each(function () {
this.value = this.checked ? 'on' : 'off';
var resolution = $(this).parent().find('span').text();
resolutiontemp[resolution] = this.value;
})
resolution.push(resolutiontemp);
console.log(JSON.stringify(resolution));
});
演示: 小提琴
使用下面的代码片段可获得预期的结果。
的HTML
<table>
<tr>
<td>
<input type="checkbox" id="res1" name="resolution[]" class="resolution" value="off"/><span>res 1 </span>
</td>
<td>
<input type="checkbox" id="res2" name="resolution[]" class="resolution" value="off"/><span>res 2</span>
</td>
<td>
<input type="checkbox" id="res3" name="resolution[]" class="resolution" value="off"/><span>res 3</span>
</td>
</tr>
<tr>
<td colspan="3"><input type="checkbox" id="selecctall" name=""/><span>Select All</span></td>
</tr>
</table>
jQuery的:
$('#selectall').click(function(event) { //on click
var resolutiontemp = {},resolution = [];
if(this.checked) {
$("input[name='resolution[]'").each(function() {
this.checked = true;
$(this).prop('value','on');
var resolutionval = $(this).val();
var resolution = $(this).parent().find('span').text();
resolutiontemp[resolution] = resolutionval;
});
}else{
$("input[name='resolution[]'").each(function() {
this.checked = false;
$(this).prop('value','off');
var resolutionval = $(this).val();
var resolution = $(this).parent().find('span').text();
resolutiontemp[resolution] = resolutionval;
});
}
resolution.push(resolutiontemp);
console.log(JSON.stringify(resolution));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.