Javascript 选中/取消选中所有复选框并添加值
我需要为每套书设置一个“全选/取消全选”框。它还应该加上总价和重量。到目前为止,我只能选中每个复选框,它可以很好地添加值,但一旦我添加了一个函数来选中所有复选框,一切都停止工作Javascript 选中/取消选中所有复选框并添加值,javascript,jquery,Javascript,Jquery,我需要为每套书设置一个“全选/取消全选”框。它还应该加上总价和重量。到目前为止,我只能选中每个复选框,它可以很好地添加值,但一旦我添加了一个函数来选中所有复选框,一切都停止工作 //检查所有函数(注释它,因为它不工作) //$(“#checkAll”).change(函数(){ //$(“输入:复选框”).prop('checked',$(this.prop(“checked”)); //}); //增加重量和价格 var inputs=document.getElementsByClassN
//检查所有函数(注释它,因为它不工作)
//$(“#checkAll”).change(函数(){
//$(“输入:复选框”).prop('checked',$(this.prop(“checked”));
//});
//增加重量和价格
var inputs=document.getElementsByClassName('sum'),
totalElement=document.getElementById('payment-total'),
weightElement=document.getElementById('payment-weight'),
总价=0,
总重量=0;
对于(变量i=0;i
检查所有设置A
订一本书
英语
科学类
基督徒生活
检查所有设置B
B组书籍
英语
科学类
基督徒生活
数学
重量0
价格0
HTML代码
<p>
<label><input onclick="CheckAll('group1', this);" type="checkbox" id="checkAll" /> Check all Set A</label>
</p>
<fieldset id="group1">
<legend>Set A Books</legend>
<input value="300" type="checkbox" bweight=".500" id="ENG101" class="sum" data-toggle="checkbox"> English
<input value="500" type="checkbox" bweight=".330" id="SCI101" class="sum" data-toggle="checkbox"> Science
<input value="755" type="checkbox" bweight=".633" id="CLE101" class="sum" data-toggle="checkbox"> Christian Living
</fieldset>
<p><label><input onclick="CheckAll('group2', this);" type="checkbox" id="checkAll" /> Check all Set B</label></p>
<fieldset id="group2">
<legend>Set B Books</legend>
<input value="245" type="checkbox" bweight=".845" id="ENG202" class="sum" data-toggle="checkbox"> English
<input value="534" type="checkbox" bweight=".734" id="SCI202" class="sum" data-toggle="checkbox"> Science
<input value="623" type="checkbox" bweight=".257" id="CLE202" class="sum" data-toggle="checkbox"> Christian Living
<input value="954" type="checkbox" bweight=".845" id="MAT101" class="sum" data-toggle="checkbox"> Math
</fieldset>
<p></p>
<div class="card-charge-info">
Weight <span id="payment-weight">0</span>
</div>
<div class="card-charge-info">
Price <span id="payment-total">0</span>
</div>
检查所有设置A
订一本书
英语
科学类
基督徒生活
检查所有设置B
B组书籍
英语
科学类
基督徒生活
数学
重量0
价格0
Java脚本代码
var inputs = document.getElementsByClassName('sum'),
totalElement = document.getElementById('payment-total'),
weightElement = document.getElementById('payment-weight'),
totalPrice = 0,
totalWeight = 0;
for (var i = 0; i < inputs.length; i++) {
inputs[i].onchange = function () {
if (this.checked) {
totalPrice += parseFloat(this.value);
totalWeight += parseFloat(this.getAttribute('Bweight'));
} else {
totalPrice -= parseFloat(this.value);
totalWeight -= parseFloat(this.getAttribute('Bweight'));
}
totalElement.innerHTML = totalPrice.toFixed(2);
weightElement.innerHTML = totalWeight.toFixed(2);
}
}
function CheckAll(divId, sourceCheckbox) {
divElement = document.getElementById(divId);
inputElements = divElement.getElementsByTagName('input');
for (i = 0; i < inputElements.length; i++) {
if (inputElements[i].type != 'checkbox')
continue;
inputElements[i].checked = sourceCheckbox.checked;
if (sourceCheckbox.checked) {
totalPrice += parseFloat(inputElements[i].value);
totalWeight += parseFloat(inputElements[i].getAttribute('Bweight'));
} else {
totalPrice -= parseFloat(inputElements[i].value);
totalWeight -= parseFloat(inputElements[i].getAttribute('Bweight'));
}
totalElement.innerHTML = totalPrice.toFixed(2);
weightElement.innerHTML = totalWeight.toFixed(2);
}
}
var inputs=document.getElementsByClassName('sum'),
totalElement=document.getElementById('payment-total'),
weightElement=document.getElementById('payment-weight'),
总价=0,
总重量=0;
对于(变量i=0;i
HTML代码
<p>
<label><input onclick="CheckAll('group1', this);" type="checkbox" id="checkAll" /> Check all Set A</label>
</p>
<fieldset id="group1">
<legend>Set A Books</legend>
<input value="300" type="checkbox" bweight=".500" id="ENG101" class="sum" data-toggle="checkbox"> English
<input value="500" type="checkbox" bweight=".330" id="SCI101" class="sum" data-toggle="checkbox"> Science
<input value="755" type="checkbox" bweight=".633" id="CLE101" class="sum" data-toggle="checkbox"> Christian Living
</fieldset>
<p><label><input onclick="CheckAll('group2', this);" type="checkbox" id="checkAll" /> Check all Set B</label></p>
<fieldset id="group2">
<legend>Set B Books</legend>
<input value="245" type="checkbox" bweight=".845" id="ENG202" class="sum" data-toggle="checkbox"> English
<input value="534" type="checkbox" bweight=".734" id="SCI202" class="sum" data-toggle="checkbox"> Science
<input value="623" type="checkbox" bweight=".257" id="CLE202" class="sum" data-toggle="checkbox"> Christian Living
<input value="954" type="checkbox" bweight=".845" id="MAT101" class="sum" data-toggle="checkbox"> Math
</fieldset>
<p></p>
<div class="card-charge-info">
Weight <span id="payment-weight">0</span>
</div>
<div class="card-charge-info">
Price <span id="payment-total">0</span>
</div>
检查所有设置A
订一本书
英语
科学类
基督徒生活
检查所有设置B
B组书籍
英语
科学类
基督徒生活
数学
重量0
价格0
Java脚本代码
var inputs = document.getElementsByClassName('sum'),
totalElement = document.getElementById('payment-total'),
weightElement = document.getElementById('payment-weight'),
totalPrice = 0,
totalWeight = 0;
for (var i = 0; i < inputs.length; i++) {
inputs[i].onchange = function () {
if (this.checked) {
totalPrice += parseFloat(this.value);
totalWeight += parseFloat(this.getAttribute('Bweight'));
} else {
totalPrice -= parseFloat(this.value);
totalWeight -= parseFloat(this.getAttribute('Bweight'));
}
totalElement.innerHTML = totalPrice.toFixed(2);
weightElement.innerHTML = totalWeight.toFixed(2);
}
}
function CheckAll(divId, sourceCheckbox) {
divElement = document.getElementById(divId);
inputElements = divElement.getElementsByTagName('input');
for (i = 0; i < inputElements.length; i++) {
if (inputElements[i].type != 'checkbox')
continue;
inputElements[i].checked = sourceCheckbox.checked;
if (sourceCheckbox.checked) {
totalPrice += parseFloat(inputElements[i].value);
totalWeight += parseFloat(inputElements[i].getAttribute('Bweight'));
} else {
totalPrice -= parseFloat(inputElements[i].value);
totalWeight -= parseFloat(inputElements[i].getAttribute('Bweight'));
}
totalElement.innerHTML = totalPrice.toFixed(2);
weightElement.innerHTML = totalWeight.toFixed(2);
}
}
var inputs=document.getElementsByClassName('sum'),
totalElement=document.getElementById('payment-total'),
weightElement=document.getElementById('payment-weight'),
总价=0,
总重量=0;
对于(变量i=0;i
试试这个:
$("#checkAll").change(function() {
$(':checkbox').prop('checked', true); //for check all checkboxes
$(':checkbox').prop('checked', false); //for uncheck all checkboxes
});
试试这个:
$("#checkAll").change(function() {
$(':checkbox').prop('checked', true); //for check all checkboxes
$(':checkbox').prop('checked', false); //for uncheck all checkboxes
});
请检查这个代码
$(文档).ready(函数(){
$(“#AllsetA”).change(函数(){
$(“.setA”).prop('checked',$(this.prop('checked'));
changeval();
});
$(“#AllsetB”).change(函数(){