<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>获取checkbox在table中对应的行数</title>
<style type="text/css">
*{margin:0;padding:0}
.tables{width:500px;background: #f0f0f0;margin:50px auto;font:12px/21px Arial, Helvetica, sans-serif}
.tables td{border: 2px solid #fff}
</style>
</head>
<body>
<table class="tables">
<caption>获取checkbox在table中对应的行数</caption>
<tr>
<td><input type="checkbox" class="chk" /> </td>
</tr>
<tr>
<td><input type="checkbox" class="chk" /> </td>
</tr>
<tr>
<td><input type="checkbox" class="chk" /> </td>
</tr>
<tr>
<td><input type="checkbox" class="chk" /> </td>
</tr>
<tr>
<td><input type="checkbox" class="chk" /> </td>
</tr>
</table>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
var total_tr=$(".tables").find("tr"); //获得table中tr 的总行数
$(".tables").click(function(event){
event = window.event || event;
var target = event.target || event.srcElement;
if(target.type == "checkbox" && target.checked == true){
var this_tr=$(target).parent().parent();//获得当前点击的checkbox 所在tr
var index=total_tr.index(this_tr); //获取该tr在整个table中 位置
alert('此checkbox在table 中对应的行数是:'+index+' (这只是索引值)');
}
});
</script>
</body>
</html>
追问如何点击按钮后得到所有被选中的行数呢
追答一样的嘛.....
追问已经有人给我讲解了 没有你那么复杂 谢谢了