怎么样点一行表格,把checkbox给勾上

599899 2006-02-16 04:27:22
我想实现点一行表格的任何位置,把这一行前面的checkbox勾上,同时表格变成选中的颜色
,再点一下把checkbox勾去掉,表格变成原来的颜色.

-------------------------------------------------------------------
<table border="1" width="200">
<tr onMouseOver="this.style.backgroundColor='#D7E8F8'" onMouseOut="this.style.backgroundColor='#FFFFFF'">
<td><input type="checkbox" name="a"></td><td>123</td></tr>
<tr onMouseOver="this.style.backgroundColor='#D7E8F8'" onMouseOut="this.style.backgroundColor='#FFFFFF'">
<td><input type="checkbox" name="a"></td><td>456</td></tr>
</table>
------------------------------------------------------------------
...全文
137 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
chouchy 2006-02-16
  • 打赏
  • 举报
回复
<table border="1" width="200">
<tr onclick="mm(this)">
<td><input id="1" type="checkbox" name="a"></td><td>123</td>
</tr>
<tr onclick="mm(this)">
<td><input id="2" type="checkbox" name="a"></td><td>456</td>
</tr>
</table>
<SCRIPT LANGUAGE="JavaScript">
//以下改自meizz(梅花雪)提供的函数,但比他的高效。
function mm(tr)
{
var me = event.srcElement;
if(me.tagName!="INPUT")
{
var me = tr.firstChild.firstChild;
me.checked=!me.checked;
}
tr.style.backgroundColor= me.checked ? "blue" : "";
}
</SCRIPT>
meizz 2006-02-16
  • 打赏
  • 举报
回复
我想实现点一行表格的任何位置,把这一行前面的checkbox勾上,同时表格变成选中的颜色
,再点一下把checkbox勾去掉,表格变成原来的颜色.
------------------------------------------------
<table border="1" width="200">
<tr onclick="mm(this)">
<td><input id="1" type="checkbox" name="a"></td><td>123</td>
</tr>
<tr onclick="mm(this)">
<td><input id="2" type="checkbox" name="a"></td><td>456</td>
</tr>
</table>
<SCRIPT LANGUAGE="JavaScript">
function mm(tr)
{
var me = event.srcElement;
if(event.srcElement.tagName!="INPUT")
{
var a = tr.getElementsByTagName("INPUT");
for(var i=0; i<a.length; i++)
if(a[i].name=="a"){a[i].checked=!a[i].checked;me=a[i];break;}
}
tr.style.backgroundColor= me.checked ? "red" : "";
}
</SCRIPT>
mingxuan3000 2006-02-16
  • 打赏
  • 举报
回复
<table border="1" width="200">
<tr onclick="if(document.getElementById('1').checked == true){document.getElementById('1').checked = false;this.style.backgroundColor='#FFFFFF'}else{document.getElementById('1').checked = true;this.style.backgroundColor='#F00';}">
<td><input id="1" type="radio" name="a1"></td><td>123</td></tr>
<tr onclick="if(document.getElementById('2').checked == true){document.getElementById('2').checked = false;this.style.backgroundColor='#FFFFFF'}else{document.getElementById('2').checked = true;this.style.backgroundColor='#F00';}">
<td><input id="2" type="radio" name="a2"></td><td>456</td></tr>
</table>
599899 2006-02-16
  • 打赏
  • 举报
回复
<table border="1" width="200">
<tr onclick="if(document.getElementById('1').checked == true){document.getElementById('1').checked = false;this.style.backgroundColor='#FFFFFF'}else{document.getElementById('1').checked = true;this.style.backgroundColor='#F00';}">
<td><input id="1" type="radio" name="a"></td><td>123</td></tr>
<tr onclick="if(document.getElementById('2').checked == true){document.getElementById('2').checked = false;this.style.backgroundColor='#FFFFFF'}else{document.getElementById('2').checked = true;this.style.backgroundColor='#F00';}">
<td><input id="2" type="radio" name="a"></td><td>456</td></tr>
</table>

---这样换成radio类型的就不行了,该怎么改呢???
599899 2006-02-16
  • 打赏
  • 举报
回复
greatdinosaur() 那个换成radio怎么就不行了呢?
satans18 2006-02-16
  • 打赏
  • 举报
回复
http://www.bjcan.com/hengxing/more.asp?Page=3
599899 2006-02-16
  • 打赏
  • 举报
回复
还有,有时候不灵敏,比如点某一行,点一下选中,再点一下取消,再点选种,再点取消,多试几次,就会出现点完没反映的情况(慢点儿点)
greatdinosaur 2006-02-16
  • 打赏
  • 举报
回复
<script>

</script>
<table border="1" width="200">
<tr onclick="if(document.getElementById('1').checked == true){document.getElementById('1').checked = false;this.style.backgroundColor='#D7E8F8'}else{document.getElementById('1').checked = true;this.style.backgroundColor='#F00';}" onMouseOver="if(document.getElementById('1').checked != true) this.style.backgroundColor='#D7E8F8'" onMouseOut="if(document.getElementById('1').checked != true) this.style.backgroundColor='#FFFFFF'">
<td><input id="1" type="checkbox" name="a"></td><td>123</td></tr>
<tr onclick="if(document.getElementById('2').checked == true){document.getElementById('2').checked = false;this.style.backgroundColor='#D7E8F8'}else{document.getElementById('2').checked = true;this.style.backgroundColor='#F00';}" onMouseOver="if(document.getElementById('2').checked != true) this.style.backgroundColor='#D7E8F8'" onMouseOut="if(document.getElementById('2').checked != true) this.style.backgroundColor='#FFFFFF'">
<td><input id="2" type="checkbox" name="a"></td><td>456</td></tr>
<tr onclick="if(document.getElementById('3').checked == true){document.getElementById('3').checked = false;this.style.backgroundColor='#D7E8F8'}else{document.getElementById('3').checked = true;this.style.backgroundColor='#F00';}" onMouseOver="if(document.getElementById('3').checked != true) this.style.backgroundColor='#D7E8F8'" onMouseOut="if(document.getElementById('3').checked != true) this.style.backgroundColor='#FFFFFF'">
<td><input id="3" type="checkbox" name="a"></td><td>456</td></tr>
<tr onclick="if(document.getElementById('4').checked == true){document.getElementById('4').checked = false;this.style.backgroundColor='#D7E8F8'}else{document.getElementById('4').checked = true;this.style.backgroundColor='#F00';}" onMouseOver="if(document.getElementById('4').checked != true) this.style.backgroundColor='#D7E8F8'" onMouseOut="if(document.getElementById('4').checked != true) this.style.backgroundColor='#FFFFFF'">
<td><input id="4" type="checkbox" name="a"></td><td>456</td></tr>
</table>
599899 2006-02-16
  • 打赏
  • 举报
回复
太感谢了,可不可以被选种的一整行都变成兰色的,取消选种后变回原来的白色?
hbhbhbhbhb1021 2006-02-16
  • 打赏
  • 举报
回复
如果想再点一下就不选

<table border="1" width="200">
<tr onMouseOver="this.style.backgroundColor='#D7E8F8'" onMouseOut="this.style.backgroundColor='#FFFFFF'" onclick="show(this.childNodes[0].childNodes[0])">
<td><input type="checkbox" name="a"></td><td>123</td></tr>
<tr onMouseOver="this.style.backgroundColor='#D7E8F8'" onMouseOut="this.style.backgroundColor='#FFFFFF'" onclick="show(this.childNodes[0].childNodes[0])">
<td><input type="checkbox" name="a"></td><td>456</td></tr>
</table>
<script language=javascript>
function show(obj)
{
if(obj.checked==true)
{
obj.checked=false;
}
else
{
obj.checked=true;
}
}
</script>
hbhbhbhbhb1021 2006-02-16
  • 打赏
  • 举报
回复
<table border="1" width="200">
<tr onMouseOver="this.style.backgroundColor='#D7E8F8'" onMouseOut="this.style.backgroundColor='#FFFFFF'" onclick="this.childNodes[0].childNodes[0].checked=true">
<td><input type="checkbox" name="a"></td><td>123</td></tr>
<tr onMouseOver="this.style.backgroundColor='#D7E8F8'" onMouseOut="this.style.backgroundColor='#FFFFFF'" onclick="this.childNodes[0].childNodes[0].checked=true">
<td><input type="checkbox" name="a"></td><td>456</td></tr>
</table>

87,992

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧