根据复选框选择启用/禁用表

时间:2014-03-12 06:56:32

标签: javascript jquery html css

需要根据复选框选择从表的TD添加/删除类。默认情况下,所有TD都将被隐藏。在选中复选框时,我需要单独启用相应的列。

JSFiddle:http://jsfiddle.net/eVj8V/2/

代码:

<html>

<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>

    <style>
        .hideThis {display: none;}
        td, th {
                    border: thin solid;
            }
    </style>

    <script>

        function constrctTable() {
            var TableRef = document.getElementById("TableConstruction");
            TableRef.innerHTML = "";
            var table = "";
            table += "<table>";
            table += "<tr style='border: inherit;' id='tableColumns'>";
            table += "<td>S.No</td>";
            table += "<td>Name</td>";
            table += "<td class=hideThis>Employee No</td>";
            table += "<td class=hideThis>Manager No</td>";
            table += "<td class=hideThis>Clerk No</td>";


            table += "</tr>";
            for(i=0; i<5; i++) {
                table += "<tr class=recordRow>";
                table += "<td>"+i+"</td>";
                table += "<td>Raj "+i+"</td>";
                table += "<td class=hideThis name='Employee'>"+ i +"</td>";
                table += "<td class=hideThis name='Manager'>"+ i +"</td>";
                table += "<td class=hideThis name='Clerk'>"+ i +"</td>";
                    table += "</tr>";
            }
            table += "</table>";

            TableRef.innerHTML = table;
        }


        function enableEmployee(enableRef) {
            var compRef = document.getElementById('employee').checked;
            if(compRef){
                $('.hideThis:contains(' + enableRef + ')').removeClass('hideThis');
                $('.recordRow td.hideThis[name=' +enableRef+ ']').removeClass('hideThis');
            } else {
                $('.hideThis:contains(' + enableRef + ')').addClass('hideThis');
                $('.recordRow td.hideThis[name !=' +enableRef+ ']').addClass('hideThis'); 
            }
        }


        function enableManager(enableRef) {
            var compRef = document.getElementById('manager').checked;
            if(compRef){
                $('.hideThis:contains(' + enableRef + ')').removeClass('hideThis');
                $('.recordRow td.hideThis[name=' +enableRef+ ']').removeClass('hideThis');
            } else {
                $('.hideThis:contains(' + enableRef + ')').addClass('hideThis');
                $('.recordRow td.hideThis[name !=' +enableRef+ ']').addClass('hideThis'); 
            }
        }

        function enableClerk(enableRef) {
            var compRef = document.getElementById('clerk').checked;
            if(compRef){
                $('.hideThis:contains(' + enableRef + ')').removeClass('hideThis');
                $('.recordRow td.hideThis[name=' +enableRef+ ']').removeClass('hideThis');
            } else {
                $('.hideThis:contains(' + enableRef + ')').addClass('hideThis');
                $('.recordRow td.hideThis[name !=' +enableRef+ ']').addClass('hideThis'); 
            }
        }

    </script>

</head>

<body onload="constrctTable();">

<div id="TableConstruction"> </div>

<input type='checkbox'  id='employee' onclick='enableEmployee("Employee")'>Employee</input>
<input type='checkbox'  id='manager' onclick='enableManager("Manager")'>Manager</input>
<input type='checkbox'  id='clerk'  onclick='enableClerk("Clerk")'>Clerk</input>

</body>


</html>

提前致谢。

1 个答案:

答案 0 :(得分:2)

我修改了你的代码以使其变得简单。试试这个。

$(document).on('change','input[type="checkbox"]',function(){
var compRef = this.checked;
if(compRef){
  $("tr td:contains('"+$(this).attr('name')+"')").removeClass('hideThis');
  $("tr td[name='"+$(this).attr('name')+"']").removeClass('hideThis');
}else{
  $("tr td:contains('"+$(this).attr('name')+"')").addClass('hideThis') 
  $("tr td[name='"+$(this).attr('name')+"']").addClass('hideThis');
}});

<强> Working Demo