zoukankan      html  css  js  c++  java
  • js获得checkbox选中值及input后面的文本

    原文:http://blog.csdn.net/u014079773/article/details/51865596

    js如何获得多个checkbox选中的值及input后面所跟的文本

    <!doctype html>
    <html lang="en">
     <head>
      <meta charset="UTF-8">
      <meta name="Author" content="">
      <meta name="Keywords" content="">
      <meta name="Description" content="">
      <title>验证js获得多个checkbox选中值及input后面所跟文本</title>
       <script type="text/javascript">
           function clickBjjb() {
           var checkboxValue= new Array();
           var checkboxText= new Array();
           var checkboxStr=document.getElementsByName("bjjb");  
           for(var i=0; i<checkboxStr.length; i++){
            if(checkboxStr[i].checked){
                    //alert(checkboxStr[i].value+","+checkboxStr[i].nextSibling.nodeValue);
                checkboxValue.push(checkboxStr[i].value);
                        checkboxText.push(checkboxStr[i].nextSibling.nodeValue);
             }
            } 
            //输出值和文本
             alert("checkboxValue:"+checkboxValue);
                 alert("checkboxText:"+checkboxText);
               //把获得的数据转换为字符串传递到后台           
               checkboxValue=checkboxValue.toString();
             checkboxText=checkboxText.toString();
             window.location='某Action/netWorkingUpdate?checkboxValue='+checkboxValue+"&checkboxText="+checkboxText;
    
      } 
    </script> 
    </head> 
    <body> 
       <form id="checkboxform" method="post" action="">
        <input type="checkbox" name="bjjb" value="1">交通事故<br>
        <input type="checkbox" name="bjjb" value="2">自然灾害<br>
        <input type="checkbox" name="bjjb" value="3">恶劣天气<br>
       <input type="checkbox" name="bjjb" value="4">严重违法行为<br>
       <input type="checkbox" name="bjjb" value="5">路面损毁<br>
      <input type="button" onclick="clickBjjb()" value="报警级别" />
     </form> 
    </body>
    </html>

    后台获得参数为:

    //获得的均为数组值。

    String checboxValues=request.getParameter("checboxValue");

    String checboxTexts=request.getParameter("checboxText");

    //得到每个具体值

    String checboxValue=checboxValues.split(",");

    String checboxText=checboxTexts.split(",");

    备注:

    1:nextSibling是获得当前对象的下一个对象,nodeValue是返回一个节点的值

    2:使用该方法必须保证文本值在input后面,否则checkboxStr[i].nextSibling.nodeValue获取不到文本值

  • 相关阅读:
    IsBadReadPtr|IsBadWritePtr调试崩溃
    VSCode配置python调试环境
    Visual Studio Code 如何编写运行 C、C++ 程序?
    使用nginx做反向代理
    Win10环境下配置VScode的C++编译环境
    关于java 获取 html select标签 下拉框 option 文本内容 隐藏域
    【VSCode】Windows下VSCode编译调试c/c++【更新 2018.03.27】
    VS Code 配置 C/C++ 环境
    改变你一生的编辑器:VSCode使用总结
    CentOS7,安装Tomcat8.5、JDK1.8,并设置开机启动(Linux CentOS Tomcat、JDK+Tomcat、Tomcat开机自启动)
  • 原文地址:https://www.cnblogs.com/shihaiming/p/7356230.html
Copyright © 2011-2022 走看看