首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

/新手/checkbox的checked判断有关问题

2012-02-03 
/新手/checkbox的checked判断问题代码:htmlbodyformname sample tbodytrth question /th

/新手/checkbox的checked判断问题
代码:
<html>
<body>
<form   name= "sample ">
<tbody>
<tr>
<th> question </th>
</tr>
<tr>
<td>
<input   type= "checkbox "   value= "a "   name= "chkb "   /> a
<input   type= "checkbox "   value= "b "   name= "chkb "   /> b
<input   type= "checkbox "   value= "c "   name= "chkb "   /> c
</td>
</tr>
</tbody>
</form>
<script>
if   (document.sample.chkb[0].checked==true)
{
alert( 'check ')
}
else{
alert( 'undef ')
}
</script>
</body>
</html>
我想实现的效果是在页面载入后,只要改变chkb[0]就即刻判断,并运行相应代码,而非只是在载入时一次性判断.
请问如何解决,谢谢.

[解决办法]
<html>
<body>
<form name= "sample ">
<tbody>
<tr>
<th> question </th>
</tr>
<tr>
<td>
<input type= "checkbox " value= "a " name= "chkb " onclick= "selectChange(0); "/> a
<input type= "checkbox " value= "b " name= "chkb " onclick= "selectChange(1); "/> b
<input type= "checkbox " value= "c " name= "chkb " onclick= "selectChange(2); "/> c
</td>
</tr>
</tbody>
</form>
<script>
function selectChange(num)
{
if (document.sample.chkb[num].checked==true)
{
alert( 'check ')
}
else{
alert( 'undef ')
}
}
</script>
</body>
</html>
是不是要这样的效果?
[解决办法]
<html>
<body>
<form name= "sample ">
<tbody>
<tr>
<th> question </th>
</tr>
<tr>
<td>
<input type= "checkbox " value= "a " name= "chkb " onclick= "javascript:cbchecked(0); " /> a
<input type= "checkbox " value= "b " name= "chkb " onclick= "javascript:cbchecked(1); " /> b
<input type= "checkbox " value= "c " name= "chkb " onclick= "javascript:cbchecked(2); " /> c
</td>
</tr>
</tbody>
</form>
<script>
function cbchecked(str){
if (document.sample.chkb[str].checked==true)
{
alert( 'check ')
}
else{
alert( 'undef ')
}
}
</script>
</body>
</html>

[解决办法]
<input type= "checkbox " value= "a " name= "chkb " onclick= "if(this.checked){alert( 'checked ')}else{alert( 'no ')} "/>

热点排行