js获取checkbox被选中的value值

来源:互联网 发布:日本黑科技 知乎 编辑:程序博客网 时间:2024/05/29 15:36

js获取选中checkbox的value:

$("input[type='checkbox']:checked").value;


js获取选中checkbox的旁边的文本:

$("input[type='checkbox']:checked").nextSibling.nodeValue;

解释:

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


例:

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
  <script src="jquery.min.js" type="text/javascript"></script>
  <script>
 function chk(){
  var str=$("input[type='checkbox']:checked");
  var objarray=str.length;
  var chestr="";

  var nextStr = "";
  for (i=0;i<objarray;i++)
  {
    if(str[i].checked == true)
    {
     chestr+=str[i].value+",";

     nextStr+=str[i].nextSibling.nodeValue;
    }
  }
  alert(chestr+"---"+nextStr);
 }
  </script>
 </head>
 <body>
   <input id='chk' name='chk'  type='checkbox' value='A'/>AA
   <input id='chk' name='chk'  type='checkbox' value='B'/>BB
   <input id='chk' name='chk'  type='checkbox' value='C'/>CC
   <input id='chk' name='chk'  type='checkbox' value='D'/>DD
   <input id='chk' name='chk'  type='checkbox' value='E'/>EE
   <input id='chk' name='chk'  type='checkbox' value='F'/>FF
   <input type='button' value='btn' onclick='chk();'/>
 </body>
</html>

0 0