admin管理员组文章数量:1277887
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 p=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)
p = p + document.getElementById("mpform").elements['mpay[]'][i].value;
}
document.getElementById("mpaydiv").innerHTML=p;
}
</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
Error: document.getElementById("mpform") is null Source File: index.php?page=fakturi Line: 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?
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 p=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)
p = p + document.getElementById("mpform").elements['mpay[]'][i].value;
}
document.getElementById("mpaydiv").innerHTML=p;
}
</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
Error: document.getElementById("mpform") is null Source File: index.php?page=fakturi Line: 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?
Share Improve this question asked Aug 5, 2011 at 20:11 DreamWaveDreamWave 1,9403 gold badges28 silver badges63 bronze badges6 Answers
Reset to default 2The id of your form must be mpform. You have name="mpform". Add id="mpform".
Also, for efficiency sake, you should change your function to this. Using multiple getElementById for grabbing the same element is very inefficient.
<script type="text/javascript">
function mpfunc()
{
var p=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)
p = p + parseInt(elements[i].value);
}
document.getElementById("mpaydiv").innerHTML=p;
}
</script>
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]
Should be like this
<form name="mpform" id="mpform"></form>
You try to get Element By Id but you specified a name for it.
<form id="mpform">
Is it possible that the function is running before the form element has finished loading? Try running the function as part of the window.onload
event.
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.
<script type="text/javascript">
function mpfunc()
{
var p=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)
p = p + parseInt(elements[i].value);
}
document.getElementById("mpaydiv").innerHTML=p;
}
</script>
本文标签: Get checkbox values in javascriptStack Overflow
版权声明:本文标题:Get checkbox values in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741216381a2360122.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论