简体   繁体   English

复选框值和数字框的总和

[英]Sum of checkboxes values and number boxes

I am having trouble trying that this script sums, on the one hand, the values of all the marked checkboxes (all with name="PV". The sum should be "PV1"), and on the other hand, the values manually written in the relevant input number boxes (all with name="pvo". The sum should be PV2). 我在尝试将此脚本求和时遇到麻烦,一方面,将所有已标记的复选框的值相加(所有名称都为“ PV”。总和应为“ PV1”),另一方面,手动写入的值在相关的输入数字框中(所有名称都为“ pvo”。总和应为PV2)。 After that, with those values it should calculate other numbers (PB1 and PB2). 之后,应使用这些值计算其他数字(PB1和PB2)。 However it doesn't return anything. 但是,它不返回任何内容。 What am I doing wrong? 我究竟做错了什么?

<script language="JavaScript" type="text/javascript">
var PV = document.getElementsByName("PV")
var PVO = document.getElementsByName("pvo")
var PV1 = document.getElementById("PV1")
var PV2 = document.getElementById("PV2")
var PB1 = document.getElementById("PB1")
var PB2 = document.getElementById("PB2")

function clickCh(form){calcpv(this.form) ; calcpvo(this.form) ; calculate(this.form)}

function trunc(n){return ~~n; }

function calcpv(form){for(i=0 ; i < PV.length ; i++)
{if(PV[i].checked == true) {PV1.value += Number(PV[i].value)} }}

function calcpvo(form){for(i=0 ; i < PVO.length ; i++)
{PV2.value += Number(PVO[i].value=)} }

function calculate(form){if(PV1>PV2){PB1.value = trunc(10 + (PV1.value*1 - PV2.value*1 -1)/150)}
else {PB1.value = trunc(10 + (PV1.value*1 - PV2.value*1 +1)/150)}; PB2.value = 20 - PB1.value
}


</script>

A relevant sample of my HTML code (there are many table rows similar to this one) 我的HTML代码的相关示例(有很多与此类似的表行)

<td><input type="checkbox" name="PV" value=70>
<input type="checkbox" name="PV" value=70>
<input type="checkbox" name="PV" value=70>
<input type="checkbox" name="PV" value=70></td>
<td><input type="text"></td><td><input type="number" name="pvo"></td></tr>
<input id="PV1" type="text" name="" readonly="readonly">
<input id="PV2" type="text" name="" readonly="readonly">
<input type="button" onclick="clickCh(this.form)" value="calcular"><br>
<input id="PB1" type="text" name="" readonly="readonly">
<input id="PB2" type="text" name="" readonly="readonly"><br>

If you like to use jQuery than it will easy thing: 如果您喜欢使用jQuery,它将比这更容易:

$('input[name=PV]:checked').each(function(){

/// do calculation here.

});

What you're doing wrong: You seem to be referencing an array that doesn't exist. 您做错了什么:您似乎在引用一个不存在的数组。

if(PV[i].checked == true)

I believe you mean to check this: 我相信你的意思是检查一下:

if("PV" + i.checked == true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM