作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想做的是显示每个选定复选框的总数,例如:如果我选择一个值为 100 的复选框,而不是选择另一个值为 200 的复选框,我将得到 300 作为输出,如果我取消选择它将总去吧。我尽我所能但没有结果,需要一些帮助。
这是在html之上我使用 PHP,所以我可以更快地更改值
<?php
//basisprijs en titel die erbij komen, Dit moet aangepast worden.
$basisPrijs=
array(
array("titel" => 'template1', "prijs" => '100'),
array("titel" => 'template2', "prijs" => '200'),
array("titel" => 'template3', "prijs" => '300'),
array("titel" => 'template4', "prijs" => '400')
);
//onderdelen en titel die erbij komen, Dit moet aangepast worden.
$onderdelen=
array(
array("titel" => 'Table', "prijs" => '100'),
array("titel" => 'Checkbox', "prijs" => '200'),
array("titel" => 'List', "prijs" => '300'),
array("titel" => 'Selectbox', "prijs" => '400')
);
$titel="Bereken tool";
$check="Bestellen";
?>
这是在 head 和 Jquery 之后
<body>
<!-- <script type="text/javascript"></script> -->
<div class="container">
<div id="content">
<h3><?php echo $titel; ?></h3>
<form action="index.php" method="post">
<div class="checkbox">
<?php foreach($onderdelen as $key => $value)
{echo "<div class='checkbox'>
<label><input class='check' type='checkbox' value='".$onderdelen[$key]["prijs"]."'
name='".$onderdelen[$key]["titel"]."'>"
.$onderdelen[$key]["titel"]."</label></div>"
;}?>
</div>
</form>
<div>
<button type="button" class="btn btn-default"><?php echo $check; ?></button>
<p id="output">output:</p>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$(".check").change(function() {
var prijs= this.checked ? this.value : '';
$("#output").text(prijs);
});
});
</script>
最佳答案
试试这个(我没试过,但就是这个概念):
<script>
$(document).ready(function()
{
$('.check').change(function()
{
var totalPrijs=0;
$('.check').each(function () {
if (this.checked) {
totalPrijs+=parseInt($(this).val());
}
});
$("#output").text(totalPrijs);
});
});
</script>
关于javascript - jQuery 复选框计算总值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33807872/
我是一名优秀的程序员,十分优秀!