一个表单中出现很多的checkbox,当客户端勾选其中的项目后,会将被勾选的项目名称复制到下面的一个textarea中,我的代码是:  function chksel(str){  
  var   str   =   str   .split('-'); 
  var   chkbox=document.getElementsByName(str);   
  var   v="";   
  for(var   i=0;i<chkbox.length;i++){   
  if(chkbox[i].checked)   v+=","+chkbox[i].value;   
  }   
  var selid = 'myid'+str[1];
  
  document.getElementById([selid]).value =v.replace(/^,{1}/,"");
  document.getElementById([selid]).style.display='block';
  }  问题就是:这个代码能进行正常的选择,但是是否可以做到这样:按照客户勾选的顺序出现在textarea中?
也就是:这段代码勾选的结果是,按照checkbox出现在页面中的实际位置来选择了,能否按照客户端手动选择,比如先选择第2个checkbox,这时textarea中出现的第一个应该是checkbox2的value;接着再勾选checkbox1,textarea中紧接着出现checkbox1的值接在后面?

解决方案 »

  1.   


    <input type="checkbox" id="checkbox1" value="1111" />
        <input type="checkbox" id="checkbox2" value="2222" />
        <input type="checkbox" id="checkbox3" value="3333" />
        <input type="checkbox" id="checkbox4" value="4444" />
        <input type="checkbox" id="checkbox5" value="5555" />
        <input type="checkbox" id="checkbox6" value="6666" />
        <textarea id="tarea"></textarea><script type="text/javascript">
    $("input[type='checkbox']").click(function(){
        $("#tarea").val($("#tarea").val()+" "+$(this).val())
    });
    </script>
      

  2.   

    ls兄弟你的代码是jquery的吗?
      

  3.   

    是呀! 怎么不用jquery?那写js咯,是要实现:
    选择第2个checkbox,这时textarea中出现的第一个应该是checkbox2的value;接着再勾选checkbox1,textarea中紧接着出现checkbox1的值接在后面
    是不?
      

  4.   


        <input type="checkbox" id="checkbox1" value="1111" onclick="check(this)" />
        <input type="checkbox" id="checkbox2" value="2222" onclick="check(this)" />
        <input type="checkbox" id="checkbox3" value="3333" onclick="check(this)" />
        <input type="checkbox" id="checkbox4" value="4444" onclick="check(this)" />
        <input type="checkbox" id="checkbox5" value="5555" onclick="check(this)" />
        <input type="checkbox" id="checkbox6" value="6666" onclick="check(this)" />
        <textarea id="tarea"></textarea><script type="text/javascript">function check(ele)
        {
            if(ele.checked)
            {
                document.getElementById("tarea").value=document.getElementById("tarea").value+" "+ele.value
            }
            else
            {
                document.getElementById("tarea").value=document.getElementById("tarea").value.replace(" "+ele.value,"");
            }
        }</script>
      

  5.   

    我的意思是:按照客户端实际勾选的顺序进行排序,不是选择了2就1,1就2,这样就不对了……呵呵
    就是加入有10个checkbox,我在一楼贴出来的代码,是会按照这10个checkbox在网页中出现的实际位置来排序了,而没有按照客户端任意勾选进行排序,是这个意思
      

  6.   

    <script type="text/javascript">
    function btnclick(obj)
    {
    if(obj.checked)
    {
    document.getElementById("mytext").value+=obj.value+"\n";
    }
    }
    </script>
    </head><body>
    <input type="checkbox" id="1" value="1111111"  tabindex="0"  o onclick="btnclick(this);"/>第一个 <br/>
    <input type="checkbox" id="2" value="2222222"  tabindex="0" onclick="btnclick(this);"/>第一个 <br/>
    <input type="checkbox" id="3" value="3333333"  tabindex="0" onclick="btnclick(this);"/>第一个 <br/>
    <input type="checkbox" id="4" value="4444444"  tabindex="0" onclick="btnclick(this);"/>第一个 <br/>
    <input type="checkbox" id="5" value="5555555"  tabindex="0" onclick="btnclick(this);"/>第一个 <br/>
    <input type="checkbox" id="6" value="6666666"  tabindex="0" onclick="btnclick(this);"/>第一个 <br/>
    <hr />
    <input type="button" id="btn" value="button" onclick="btnclick()" /><br />
    <textarea id="mytext"   style="height:400px; width:400px"></textarea>
    </body>
    </html>
    希望对你有用
      

  7.   

    ls代码可用!但是如果我要删除怎么办?也就是如果勾选后,再次点击一下,取消勾选,将已经出现在textarea中的值删除掉?
      

  8.   

    你在执行的时候判断状态么?
    <script type="text/javascript">
    function btnclick(obj)
    {
    if(obj.checked)
    {
    document.getElementById("mytext").value+=obj.value+"\n";
    }
    else
    {
     //在写一下for()将最新选中的值给绑定上去就OK
    }
    }
    </script>