<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
window.onload=function()
{
var arr =document.getElementsByTagName('input')
var b = document.getElementById("btu")
var iSelect=true
b.onclick=function()
{
if(iSelect==true)
{
for(var i=0i<arr.lengthi++)
{
arr[i].checked=true
}
iSelect=false
b.value='反选'
}
else if(iSelect==false)
{
for(var i=0i<arr.lengthi++)
{
arr[i].checked=false
}
iSelect=true
b.value='全选'
}
}
}
</script>
</head>
<body>
<form>
<input type="checkbox" value="读书" name="like" />读书<br />
<input type="checkbox" value="看书" name="like" />看书<br />
<input type="checkbox" value="写书" name="like" />写书<br />
<input type="checkbox" value="听书" name="like" />听书<br />
<input type="button" id="btu" value="全选" />
</form>
</body>
</html>
你错的有点不合理啊,首先js是类c语言,但是一种弱语言,定义变量是用var 你用的是int。其次var b=document.getElementById("btu")b是你选取的元素对象,你怎么就可以直接用来判断(b=="全选")这是不对的。我在你的基础上改好了,你自己好好看看吧
<script type="text/javascript" language="javascript">function selectAllDels()
{ var allCheckBoxs = document.getElementsByName("preDelCheck")
var desc = document.getElementById("allChecked")
var selectOrUnselect=false
for(var i = 0i <allCheckBoxs.lengthi ++ ) {
if(allCheckBoxs[i].checked){
selectOrUnselect=true
break} }
if (selectOrUnselect) {
_allUnchecked(allCheckBoxs)}else {
_allchecked(allCheckBoxs)} }
function _allchecked(allCheckBoxs){
for(var i = 0i <allCheckBoxs.lengthi ++ ) {
allCheckBoxs[i].checked = true} }
function _allUnchecked(allCheckBoxs){
for(var i = 0i <allCheckBoxs.lengthi ++ ) {
allCheckBoxs[i].checked = false} } </script>
<form><input type="checkbox" name="preDelCheck" value="cream">ID1
<input type="checkbox" name="preDelCheck" value="sugar" >ID2
<input type="checkbox" name="preDelCheck" value="sugar">ID3
<input type="button" id="allChecked" value="全选/取消全选" onClick="selectAllDels()"></form>
仅供参考。<SCRIPT LANGUAGE="JavaScript">
function ck(b)
{
var input = document.getElementsByTagName("input")
for (var i=0i<input.length i++ )
{
if(input[i].type=="checkbox")
input[i].checked = b
}
}
</SCRIPT>
<INPUT TYPE="checkbox" NAME="a">1
<INPUT TYPE="checkbox" NAME="a">2
<INPUT TYPE="checkbox" NAME="a">3
<INPUT TYPE="checkbox" NAME="a">4
<INPUT TYPE="checkbox" NAME="a">5<BR>
<input type="button" onclick="ck(true)" value="全选"><input type="button" onclick="ck(false)" value="取消全选">
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)