【发布时间】:2020-06-06 05:20:12
【问题描述】:
我有一个显示数据(品牌名称、位置形式、价值等)的表格。我被指示创建由复选框组成的过滤器。
当用户选中一个复选框时,仅显示与这些复选框相关的列。如果用户选中多个复选框,则仅显示与这些复选框相关的列。
如果选中多个复选框,我遇到的问题是显示多个列。我怎样才能做到这一点?
下表为:
<table class="equipTable" id="myTable">
<thead>
<tr>
<th class="brandName">Brand Name</th>
<th class="makeModelNo">Make/Model No</th>
<th class="serialNumber">Serial Number</th>
<th class="assetTag">Asset Tag</th>
<th class="locationFrom">Location From</th>
<th class="company">Company</th>
<th class="value">Value</th>
<th class="lastModified">Last Modified</th>
</tr>
</thead>
<cfoutput query="EquipD">
<tbody>
<tr>
<td class="brandName">
<div style="float: left">
<a href="javascript:poptasticcnotes('IT_Decomm_Print.cfm?EquipID=#EquipID#');" title="Test Print">
<img src="images/printers-and-faxes-icon_sm.png" alt="Print this record" class="no-border" />
</a>
</div>
<div style="margin-left: 5px; margin-top: 10px; float: left">#BName#</div>
</td>
<td class="makeModelNo">#Model#</td>
<td class="serialNumber"><a href="mtn_EquipD.cfm?a=s&id=#EquipID#">#SNo#</a></td>
<td class="assetTag">#ATag#</td>
<td class="locationFrom">#LFrom#</td>
<td class="company">#Company#</td>
<td class="value">#CValue#</td>
<td class="lastModified">#EquipD.SubmitBy# <br>#DateFormat("#EquipD.ESubmitDt#", "mm/dd/yyyy")#</em></td>
</tr>
<tr>
<td colspan="8" id="resForSub"><strong><u>REASON</u>: </strong>#ReasonD#</td>
</tr>
</tbody>
</cfoutput>
下面的jquery代码:
$(document).ready(function() {
$('input[type="checkbox"]').click(function(){
var checkBoxes = "", chkBoxesSelected = new Array();
var idsChecked = {BrandName: 'brandName', makeModelNo: 'makeModelNo', SerialNumber: 'serialNumber', AssetTag: 'assetTag', LocationForm: 'locationFrom', Company: 'company', Value: 'value', LastModified: 'lastModified'};
//console.log(x);
$('input[type=checkbox]').each(function () {
checkBoxes += $(this).attr('id') + "-" + (this.checked ? "checked" : "not checked") + "/";
});
chkBoxesSelected = checkBoxes.split("/");
for(var x = 0; x < chkBoxesSelected.length; x++){
var temp = chkBoxesSelected[x];
for(var c = 0; c < idsChecked.length; c++){
var temp2 = idsChecked[c];
if(temp == temp2){
//show the columns that are have checkbox checked
}else{
//don't show the columns that do not have checkbox checked
}
}
}
//console.log(chkBoxesSelected);
});
});
【问题讨论】:
-
请阅读minimal reproducible example,您的问题太长,代码太多,只是为了询问“如何在选中复选框时显示/隐藏列”所有这些代码真的有必要吗?此外,您的
idsChecked对象具有无效键,它不能包含- -
@CalvinNunes:感谢您的回复。我会缩短我的问题
标签: javascript jquery checkbox