87,904
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>test</title>
<style type="text/css">
tr.hide,tr.hide td{display:none;}
</style>
</head>
<body>
性别:<select id="sex">
<option value="">不限</option>
<option value="男">男</option>
<option value="女">女</option>
</select>
姓名:<input type="text" id="name"/>
课程:<input type="text" id="course"/>
<input type="button" id="search" value="搜索" onclick="search()" />
<br>
<table>
<thead>
<tr>
<th>性别</th>
<th>姓名</th>
<th>课程</th>
</tr>
</thead>
<tbody>
<tr>
<td>男</td>
<td>张三</td>
<td>物理</td>
</tr>
<tr>
<td>女</td>
<td>王张张</td>
<td>化学</td>
</tr>
<tr>
<td>男</td>
<td>胡标张</td>
<td>物理</td>
</tr>
<tr>
<td>女</td>
<td>王五</td>
<td>数学</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
function filter(fn) {
var list = document.getElementsByTagName('table')[0].getElementsByTagName('tbody')[0].rows;
var size = list.length;
var tr;
for(var i = 0; i < size; i++) {
tr = list[i];
tr.removeAttribute('class', 'hide')
if(!fn(tr)) {
tr.setAttribute('class', 'hide');
}
}
}
function value(id) {
return document.getElementById(id).value;
}
function search() {
var sex = document.getElementById('sex').value;
var name = document.getElementById('name').value;
var course = document.getElementById('course').value;
filter(function(tr) {
if(sex && tr.cells[0].innerHTML != sex) {
return false;
}
if(name && tr.cells[1].innerHTML.indexOf(name) < 0) {
return false;
}
if(course && tr.cells[2].innerHTML.indexOf(course) < 0) {
return false;
}
return true;
});
}
</script>
</body>
</html>