日期:2014-05-16  浏览次数:20365 次

/新手/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 ')} "/>