Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果表单中有多个复选框,如何传递当前选中的所有复选框?_Javascript_Html_Checkbox - Fatal编程技术网

Javascript 如果表单中有多个复选框,如何传递当前选中的所有复选框?

Javascript 如果表单中有多个复选框,如何传递当前选中的所有复选框?,javascript,html,checkbox,Javascript,Html,Checkbox,这是我的代码: <form action="test.php" method="post"> <input type="checkbox" value="value 1" onClick="updateCards(this.value);"> <input type="checkbox" value="value 2" onClick="updateCards(this.value);"> <input type="checkbo

这是我的代码:

<form action="test.php" method="post">
    <input type="checkbox" value="value 1" onClick="updateCards(this.value);">
    <input type="checkbox" value="value 2" onClick="updateCards(this.value);">
    <input type="checkbox" value="value 3" onClick="updateCards(this.value);">
    <input type="checkbox" value="value 4" onClick="updateCards(this.value);">
    <input type="checkbox" value="value 5" onClick="updateCards(this.value);">
</form>

如何获取当前选中的所有复选框?

试试看

function getCheckedBoxes(chkboxName) {
  var checkboxes = document.getElementsByName(chkboxName);
  var checkboxesChecked = [];
  // loop over them all
  for (var i=0; i<checkboxes.length; i++) {
     // And stick the checked ones onto an array...
     if (checkboxes[i].checked) {
        checkboxesChecked.push(checkboxes[i]);
     }
  }
  // Return the array if it is non-empty, or null
  return checkboxesChecked.length > 0 ? checkboxesChecked : null;
}

// Call as
var checkedBoxes = getCheckedBoxes("mycheckboxes");

然后

函数getChcked(){ var form=document.getElementById('myform'); var chks=form.querySelectorAll('input[type=“checkbox”]”); 检查的var=[]; 对于(变量i=0;i
演示:

两种快速方法可以为表单元素提供一个ID。获取对它的引用并循环遍历它的所有子元素(这是所有复选框)并检查checked属性的值

您还可以为所有复选框指定一个类名,并使用
document.getElementsByClassName
,然后循环检查结果以检查选中的属性。然而,我认为第二种方法在IE8中可能不受支持,请不要引用我的话,但我会使用第一种方法

像这样的

<form action="test.php" method="post" id="myForm">
  <input type="checkbox" value="value 1" onClick="updateCards(this.value);">
  <input type="checkbox" value="value 2" onClick="updateCards(this.value);">
  <input type="checkbox" value="value 3" onClick="updateCards(this.value);">
  <input type="checkbox" value="value 4" onClick="updateCards(this.value);">
  <input type="checkbox" value="value 5" onClick="updateCards(this.value);">
</form>

var getValues = function() {
  var nodes = document.getElementById("myForm").children;
  var checkedValues = [],
      length = nodes.length,
      i = 0;

  for(i;i<length;i++){
    if(nodes[i].checked)
    checkedValues.push(nodes[i].value);
  }

  return checkedValues;
};

var getValues=function(){
var nodes=document.getElementById(“myForm”).children;
var checkedValues=[],
长度=节点。长度,
i=0;

对于(i;i,这里有一个简单的方法可以做到这一点:

var nodeArray = document.querySelectorAll('form input:checked'), 
    i = nodeArray.length, selected = [];
while(i--) selected.push(nodeArray[i].value);

如果只使用jQuery,这将简单得多:

  function GetChecked()
  {
     return $("input:checked");
  }

有没有办法获取值?与其按
选中。按(chks[i])
,不如按
选中。按(chks[i].value)
还有一个问题,有没有办法通过名称或id获取表单?@rotaercz如果可以分配一个,那么可以使用id
<form action="test.php" method="post" id="myForm">
  <input type="checkbox" value="value 1" onClick="updateCards(this.value);">
  <input type="checkbox" value="value 2" onClick="updateCards(this.value);">
  <input type="checkbox" value="value 3" onClick="updateCards(this.value);">
  <input type="checkbox" value="value 4" onClick="updateCards(this.value);">
  <input type="checkbox" value="value 5" onClick="updateCards(this.value);">
</form>

var getValues = function() {
  var nodes = document.getElementById("myForm").children;
  var checkedValues = [],
      length = nodes.length,
      i = 0;

  for(i;i<length;i++){
    if(nodes[i].checked)
    checkedValues.push(nodes[i].value);
  }

  return checkedValues;
};
var nodeArray = document.querySelectorAll('form input:checked'), 
    i = nodeArray.length, selected = [];
while(i--) selected.push(nodeArray[i].value);
  function GetChecked()
  {
     return $("input:checked");
  }