簡體   English   中英

復選框值和數字框的總和

[英]Sum of checkboxes values and number boxes

我在嘗試將此腳本求和時遇到麻煩,一方面,將所有已標記的復選框的值相加(所有名稱都為“ PV”。總和應為“ PV1”),另一方面,手動寫入的值在相關的輸入數字框中(所有名稱都為“ pvo”。總和應為PV2)。 之后,應使用這些值計算其他數字(PB1和PB2)。 但是,它不返回任何內容。 我究竟做錯了什么?

<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>

我的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>

如果您喜歡使用jQuery,它將比這更容易:

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

/// do calculation here.

});

您做錯了什么:您似乎在引用一個不存在的數組。

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

我相信你的意思是檢查一下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM