javascript 在javascript中获取复选框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6962066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:29:21  来源:igfitidea点击:

Get checkbox values in javascript

javascript

提问by DreamWave

Roughly, this is my code:

粗略地说,这是我的代码:

<form name="mpform" id="mpform">
<input type="checkbox" onclick="mpfunc()" id="mpay" name="mpay[]" value="19" style="float:left;" />
//many of the above input tag - only the value changes with other numbers
</form>
<script type="text/javascript">
function mpfunc()
{
var comp=0;
document.getElementById("mpaydiv").style.visibility="visible";
for(var i=0; i< document.getElementById("mpform").elements['mpay[]'].length;i++){
if(document.getElementById("mpform").elements['mpay[]'][i].checked)
comp = comp + document.getElementById("mpform").elements['mpay[]'][i].value;
}
document.getElementById("mpaydiv").innerHTML=comp;
}
</script>
<div id="mpaydiv" style="position:fixed; right:0; visibility:hidden; bottom:0; border:1px solid black; background-color:white; padding:2px 2px;"></div></div>
    </div>

    <div id="footer"></div>
</div>

I have tried substituting getElementById("mpform") with form[0],mpform and others but I cant seem to access the values. With the current getelementid I get

我曾尝试用 form[0]、mpform 和其他人替换 getElementById("mpform") 但我似乎无法访问这些值。使用当前的 getelementid 我得到

Error: document.getElementById("mpform") is null Source File: index.php?page=fakturi Line: 3938

错误:document.getElementById("mpform") 为空 源文件:index.php?page=fakturi 行:3938

With mpform I get mpform is undefined as with form[0] Please help. How do I get the sum of the values of all selected checkboxes?

使用 mpform 我得到 mpform 与 form[0] 一样未定义请帮忙。如何获得所有选定复选框的值的总和?

采纳答案by cwallenpoole

I seem to recall IE having issues with the 0 index of the forms array. You can access it through, document.getElementsByName("mpform")[0]or document.forms['mpform']. To get the checkbox descendants, you might use getElementById('mpay')or document.getElementsByName("mpay")[0]or document.getElementsByName("mpform")[0].elements[0]

我似乎记得 IE 对表单数组的 0 索引有问题。您可以通过document.getElementsByName("mpform")[0]或访问它document.forms['mpform']。为了让后人复选框,您可以使用getElementById('mpay')document.getElementsByName("mpay")[0]document.getElementsByName("mpform")[0].elements[0]

回答by blejzz

Should be like this

应该是这样的

     <form name="mpform" id="mpform"></form>

回答by Kyle

The id of your form must be mpform. You have name="mpform". Add id="mpform".

您的表单的 id 必须是 mpform。你有名字=“mpform”。添加 id="mpform"。

Also, for efficiency sake, you should change your function to this. Using multiple getElementById for grabbing the same element is very inefficient.

此外,为了效率起见,您应该将您的功能更改为此。使用多个 getElementById 来获取相同的元素是非常低效的。

<script type="text/javascript">
function mpfunc()
{
   var comp=0;
   document.getElementById("mpaydiv").style.visibility="visible";
   var elements = document.getElementById("mpform").elements;
   for(var i=0; i< elements.length;i++){
      if(elements[i].type == 'checkbox' && elements[i].checked)
         comp = comp + parseInt(elements[i].value);
   }
   document.getElementById("mpaydiv").innerHTML=comp;
}
</script>

回答by Madara's Ghost

You try to get Element By Idbut you specified a name for it.

您尝试通过Id获取 Element,但您为其指定了名称。

<form id="mpform">

回答by silverstrike

Is it possible that the function is running before the form element has finished loading? Try running the function as part of the window.onloadevent.

在表单元素完成加载之前,该函数是否可能正在运行?尝试将函数作为window.onload事件的一部分运行。

回答by Kyle

I just tried this out using your html code, and it seems to work. I edited my answer above to show this, but also posted it as another answer to make sure you saw.

我只是用你的 html 代码试过了,它似乎有效。我在上面编辑了我的答案以显示这一点,但也将其作为另一个答案发布以确保您看到了。

<script type="text/javascript">
function mpfunc()
{
   var comp=0;
   document.getElementById("mpaydiv").style.visibility="visible";
   var elements = document.getElementById("mpform").elements;
   for(var i=0; i< elements.length;i++){
      if(elements[i].type == 'checkbox' && elements[i].checked)
         comp = comp + parseInt(elements[i].value);
   }
   document.getElementById("mpaydiv").innerHTML=comp;
}
</script>