日期:2014-05-17  浏览次数:20625 次

全选和反选问题
<input type="checkbox" name="chbAll" id="name="chbAll" title="全选" onclick="doSelect()">全选</div>
<input type="checkbox" name="userrights" value="common" id="ids" onclick="doSelect1()">普通用户</div>
<input type="checkbox" name="userrights" value="admin" id="ids" onclick="doSelect1()">管理员</div>

//全不选
function doSelect1()
{
var v=document.getElementById('chbAll');
var vs=document.getElementsByTagName('userrights');
var i=0;
for(;i<vs.length;i++){
if(vs[i].checked==false){
v.checked==false;
break;
}
}
if(i==vs.length){
v.checked=='checked';
}
}
//全选
function doSelect()
{
alert("!!!!!!!!!!!");
var v=document.getElementById('chbAll');
var vs=document.getElementsByTagName('userrights');
for(var i=0;i<vs.length;i++){
vs[i].checked=v.checked;
}
if(v.checked){
v.title='全不选';
}
else{
v.title='全选';
}
}

------解决方案--------------------
HTML code
<html>
<head>
<title>音乐网站那种全选反选连续播放</title>
<script language="javascript">
function MusicClass(form,chkboxName,btn1,btn2,btn3,btn4)
{
    var obj = { SelectAll:btn1, ClearAll:btn2, SelectOther:btn3, PlayMusic:btn4 };
    var box = document.getElementsByName(chkboxName);
    
    Function.prototype.ChkMusic = function ()
    {
        var me = this;
        return function ()
        {
            for (var i=0;i<box.length;i++)
            {
                me(i);
            }
        }
    }

    obj.SelectAll.onclick = function(i){ box[i].checked = true; }.ChkMusic();
    obj.ClearAll.onclick = function(i){ box[i].checked = false; }.ChkMusic();
    obj.SelectOther.onclick = function(i){ box[i].checked = !box[i].checked }.ChkMusic();
    
    obj.PlayMusic.onclick = function ()
    {
        for (var i=0;i<box.length;i++)
        {
            if (box[i].checked)
            {
                //form.submit();
                return;
            }
        }
        alert("对不起,请至少选择一首歌曲再播放!");
    }    
}

window.onload = function ()
{
    MusicClass(document.forms[0],'MusicID',document.all.All,document.all.Clear,document.all.Other,document.all.Play);
}
</script>
</head>
<body>
<form action=" method="get">
<input type='checkbox' name='MusicID' value="1" />东风破<br />
<input type='checkbox' name='MusicID' value="2" />Super Star <br />
<input type='checkbox' name='MusicID' value="3" />算你狠 <br />
<input type='checkbox' name='MusicID' value="4" />十年 <br />
<input type='checkbox' name='MusicID' value="5" />痴心绝对 (爱上痞子男 <br />
<button id="All">全选</button>
<button id="Clear">全不选</button>
<button id="Other">反选</button>
</form>
</body>
</html>