您的位置:程序门 -> web 开发 -> javascript



点击table中的tr,选中或不选中tr内的checkbox,但点击checkbox却无反应


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


点击table中的tr,选中或不选中tr内的checkbox,但点击checkbox却无反应[已结贴,结贴人:rick1125]
发表于:2007-09-06 17:05:38 楼主
写了个javascript函数,实现的功能是点击table中的一个row的任意一部分,其中有checkbox的就会被check或uncheck(相对原先的状态)
函数如下:
function   pickrow(row){
\tvar   cell   =   row.cells;
\tfor(var   i=0;i <cell.length;i++){
\t\tvar   x   =   cell.getelementsbytagname( "* ");
\t\tif(x.length   >   0){
\t\t\tfor(var   j=0;j <x.length;j++){
\t\t\t\tif(x[j].type   =   "checkbox "){
\t\t\t\t\tx[j].checked   =   !x[j].checked;
\t\t\t\t}
\t\t\t}
\t\t}
\t}
}
=================html========================
<table   class= "tblist "   border= "1 ">
<tr   class= "thead ">
<td   class= "thead "   width= "auto "> user_id </td>
<td   class= "thead "   width= "auto "> user_name </td>
<td   class= "thead "   width= "auto "> user_password </td>
<td   class= "thead "   width= "auto "> memo </td>
<td   class= " "   width= "5% "> delete </td>
</tr>
<tr   class= " "   onclick= "pickrow(this); "   onmouseover= "this.style.backgroundcolor   = '#ebebeb ';this.style.cursor= 'pointer '; "   onmouseout= "this.style.backgroundcolor   = 'white ' ">
<td   class= " "   align= "center "   width= "auto "> <a   href= "index_detail.php?user_id=1 "> 1 </a> </td>
<td   class= " "   align= "center "   width= "auto "> admin </td>
<td   class= " "   align= "center "   width= "auto "> *a4b6157319038724e3560894f7f932c8886ebfcf </td>
<td   class= " "   align= "center "   width= "auto "> admin </td>
<td   class= " "   align= "center "   width= "auto "> <input   type= "checkbox "   name= "user_id[] "   value= "1 "> </td>
</tr>
</table>
===================html====================

现在的问题是点击row的其他部分都能实现预定功能,但是点击checkbox时却不起作用了。
我想可能是checkbox在tr中,点击checkbox同样触发了tr的onclick事件,所以两个check的效果抵消了。但是如何能避免这种情况发生呢?
发表于:2007-09-06 17:11:091楼 得分:0
把每个td都写上onclick
发表于:2007-09-06 17:17:192楼 得分:0
"checkbox在tr中,点击checkbox同样触发了tr的onclick事件 "是错误的
点击checkbox触发不了tr的onclick事件

发表于:2007-09-06 17:23:273楼 得分:0
window.event.cancelbubble   =   true;
发表于:2007-09-06 18:11:524楼 得分:0
青蛙是对的,不过还是没找到如何避免的办法,每个td上都写onclick有点土。。。呵呵
发表于:2007-09-06 18:27:145楼 得分:20
<script>
function   pickrow(row){
if(event.srcelement.type== "checkbox ")   return;
var   x   =   row.getelementsbytagname( "input ");
for(var   j=0;j <x.length;j++){
if(x[j].type   =   "checkbox "){
x[j].checked   =   !x[j].checked;
}
}

}
</script>
发表于:2007-09-06 18:47:536楼 得分:0
多谢~!
发表于:2007-09-06 19:08:317楼 得分:0
"checkbox在tr中,点击checkbox同样触发了tr的onclick事件 "是错误的
点击checkbox触发不了tr的onclick事件

刚才试过了,青蛙说的不对,点击checkbox的确触发了tr的onclick事件。
jk兄提供的办法ie可行,可是firefox不吃这套。郁闷了。找了个firefox取event的函数,不过好像不管用。。。还得再试试。


快速检索

最新资讯
热门点击