//我的界面上只有一个checkbox,我想实现当用户选中checkbox时,把checkbox的值改为T,否则改为F,并把checkbox的值通过表单进行提交,我怎么试也试不通。请大家帮帮忙<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript"> 
 
function check(){
       
           var s = document.getElementsByName("checkbox");
       
           if (s.checked){
    
              document.getElementByName("checkbox").value="T"; 
           }else
           {
    
              document.getElementByName("checkbox").value="F";
           };
    
         }
      </script>
</head><body>
<form id="form1" name="form1" method="post" action="Untitled-1.html">
  <label>
    <input type="submit" name="Submit" value="提交" onclick="check();" />
  </label>
  <label>
    <input type="checkbox" name="checkbox" value="checkbox" />
     test
  </label>
</form>
</body>
</html>

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript"> 
         
            function check(){
                   
                   var s = document.getElementsByName("checkbox")[0];
               
                   if (s.checked){
            
                      s.value="T"; 
                   }else
                   {
            
                      s.value="F";
                   }
        
             }
             </script>
    </head><body>
    <form id="form1" name="form1" method="post" action="Untitled-1.html">
      <label>
        <input type="submit" name="Submit" value="提交" onclick="check();" />
      </label>
      <label>
        <input type="checkbox" name="checkbox" value="checkbox" />
         test
      </label>
    </form>
    </body>
    </html>
      

  2.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript"> 
         
            function check(){
                   
                   var s = document.getElementsByName("checkbox");
               
                    for(var i=0;i<s.length;i++)
                    {
                      if(s[i].checked)
                      {
                         s[i].value="T"
                         alert(s[i].value);
                      }
                      else
                      {
                        s[i].value="F"
                      }
                    }
        
             }
             </script>
    </head><body>
    <form id="form1" name="form1" method="post" action="Untitled-1.html">
      <label>
        <input type="submit" name="Submit" value="提交" onclick="check();" />
      </label>
      <label>
        <input type="checkbox" name="checkbox" value="checkbox" />
         test
      </label>
    </form>
    </body>
    </html>
      

  3.   

    但不建议这样写,应该使用  document.getElementById 来唯一定位 html 元素
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript"> 
         
            function check(){
                   
                   var s = document.getElementById("ckb");
               
                   if (s.checked){
            
                      s.value="T"; 
                   }else
                   {
            
                      s.value="F";
                   }
        
             }
             </script>
    </head><body>
    <form id="form1" name="form1" method="post" action="Untitled-1.html">
      <label>
        <input type="submit" name="Submit" value="提交" onclick="check();" />
      </label>
      <label>
        <input type="checkbox" id='ckb' name="checkbox" value="checkbox" />
         test
      </label>
    </form>
    </body>
    </html>
      

  4.   

    非哥速度好快啊
    LZ的问题是getElementsByName方法返回的是一个数值
      

  5.   

     var s = document.getElementsByName("checkbox")[0];
      

  6.   

    name 为 checkbox html 元素 就一个通过 document.getElementsByName("checkbox")[0] 访问
      

  7.   

    这种写法是不是有问题,当我选中checkbox时提交表单,可以获取checkbox的值为"T"。
    当我不选中checkbox时,提交表单,就获取不到checkbox的值了。HTML端我用的Sandy945的代码,这是我写的一个asp脚本获取值。<%
      response.write(Request("checkbox")) 
    %>
      

  8.   

    checkbox 只有在选中的情况下 才会 post 到 服务器端的
      

  9.   

    我再把测试的程序发一下
    我想解决的问题是:
    客户端选中/取消checkbox后,将checkbox的值修改为T/F,并且可以在asp脚本中显示checkbox的值
    HTML<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript"> 
         
            function check(){
                   
                   var s = document.getElementById("ckb");
               
                   if (s.checked){
                  
                      s.value="T"; 
                   }else
                   {
              
                      s.value="F";
                   }
        
             }
             </script>
    </head><body>
    <form id="form1" name="form1" method="post" action="test.asp">
      <label>
        <input type="submit" name="Submit" value="提交" onclick="check();" />
      </label>
      <label>
        <input type="checkbox" id='ckb' name="checkbox" value="checkbox" />
         test
      </label>
    </form>
    </body>
    </html>ASP<%
      response.write(Request("checkbox")) 
    %>
      

  10.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript"> 
         
            function check(){
                   
                   var s = document.getElementById("ckb");
                   var hid= document.getElementById("hid");
                   if (s.checked){
            
                      hid.value="T"; 
                   }else
                   {
            
                      hid.value="F";
                   }
                 }
             </script>
    </head><body>
    <form id="form1" name="form1" method="post" action="Untitled-1.html">
      <label>
        <input type="submit" name="Submit" value="提交" onclick="check();" />
      </label>
      <label>
        <input type="checkbox" id='ckb' name="checkbox" value="checkbox" />
         test
      </label>
      <input type='hidden' id='hid' name='hidName' />
    </form>
    </body>
    </html>
    <%
      response.write(Request("hidName")) 
    %>
      

  11.   

     var s = document.getElementsByName("checkbox")[0];
      

  12.   

    你这样在没有选中时是获取不到“F”值的,你可以使用另外的隐藏域来存放 “T”“F” 这样后台添加的时候始终可以获取到