[英]javascript add/remove text in div when checkbox is unchecked/checked
[英]How to add when a checkbox is checked and deduct when unchecked in Javascript when html id is a php variable
我試圖在選中復選框時從 php 數組中匯總特定列的金額,並在未選中時從總數中扣除。 我可以在選中時添加它,但在未選中時無法從總數中減少它。 事實上,每次選中或取消選中復選框時,下面的代碼都會添加。
請幫忙
<html>
<body>
<?php
$testArray = [100000,200000,300000,144444,154444,16444,174444,18444];
?>
<div>
<?php for($i=0;$i<count($testArray);$i++) { ?>
<input type="checkbox" id = <?php echo $i ?> onclick="myFunction(<?php echo $testArray[$i] ?>)" value="<?php echo $testArray[$i] ?>" ><?php echo $testArray[$i] ?> <br/>
<?php } ?>
</div>
<table>
<tr style = "display: none;" >
<td></td>
<td id = 'balance'> 0 </td>
</tr>
<tr>
<td> Total : </td>
<td id = 'balance1'> 0 </td>
</tr>
</table>
<script>
// cumulative = 2
function myFunction(x){
balance = document.getElementById('balance').textContent;
total = parseInt(balance)
total = total + x
document.getElementById('balance').textContent = total
// display with number formated
total = total.toLocaleString("en-US");
document.getElementById('balance1').textContent = total
}
</script>
</body>
</html>
<?php for($i=0;$i<count($testArray);$i++) { ?>
<script>
if (document.getElementById("<?php echo $i ?>").checked === true) total += x;
</script>
<?php } ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.