<script>
function addUser(name,obj) {
var objVal=document.all.share_user.value
    var is = obj.checked;
        if(is==true){
    if(objVal!="")
{
  objVal += ","+name;
}
else
{
  objVal= name;
}
}
else{
   if(objVal.indexOf(","+name))
    objVal= objVal.replace(","+name,"");
   else
    objVal= objVal.replace(name,"");}
</script>

解决方案 »

  1.   

    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>New Page 1</title>
    </head><body>
    <input name="share_user"><br>
    <input type=checkbox name=userBox value="user001" onclick=clickFun(this) >user001<br>
    <input type=checkbox name=userBox value="user002" onclick=clickFun(this) >user002<br>
    <input type=checkbox name=userBox value="user003" onclick=clickFun(this) >user003<br>
    <input type=checkbox name=userBox value="user004" onclick=clickFun(this) >user004<br>
    <input type=checkbox name=userBox value="user005" onclick=clickFun(this) >user005<br>
    </body></html>
    <script>
    function clickFun(obj) {
    var shareUsers=document.all.share_user.value;
    if(obj.checked && ! eval("/(^|,)"+ obj.value +"(,|$)/ig").test(shareUsers) )
      shareUsers+=obj.value+",";if(!obj.checked && eval("/(^|,)"+ obj.value +"(,|$)/ig").test(shareUsers) )
      shareUsers=shareUsers.replace(eval("/(^|,)"+ obj.value +"(,|$)/"),"$1");
      
    document.all.share_user.value=shareUsers;
    }
    </script>
      

  2.   

    飞天大哥的代码
    else{
       if(objVal.indexOf(","+name))
        objVal= objVal.replace(","+name,"");
       else
        objVal= objVal.replace(name,"");}
    这段并没有生效啊
      

  3.   

    飞天大哥你这代码text框中第一个元素前面不带逗号的就去不掉啊。
      

  4.   

    else{
       if(objVal.indexOf(","+name))//去掉带逗号的
        objVal= objVal.replace(","+name,"");
       else//去掉不带逗号的
        objVal= objVal.replace(name,"");}
      

  5.   

    可是
       if(objVal.indexOf(","+name))//去掉带逗号的
       判断条件不成立啊。无论有没有带逗号都执行
    objVal= objVal.replace(","+name,"");
      

  6.   


       if(objVal.indexOf(","+name))//去掉带逗号的
        objVal= objVal.replace(","+name,"");
       else//去掉不带逗号的
        objVal= objVal.replace(name,"");
    ========================================
    等价于:   if(objVal.indexOf(","+name)){//去掉带逗号的
        objVal= objVal.replace(","+name,"");
    }
       else{//去掉不带逗号的
        objVal= objVal.replace(name,"");
    }
      

  7.   

    sorry, 我的错!
    if(objVal.indexOf(","+name))//去掉带逗号的=============================〉 if(objVal.indexOf(","+name)!=-1)//去掉带逗号的
      

  8.   

    if(objVal.indexOf(","+name)!=-1)//去掉带逗号的
      

  9.   

    它返回的是一个索引是吧,并不是一个booble,谢谢飞天大哥