检索复选框的值以及如何添加复选框的值 [英] Retrive the Checkbox value and how to add the values of checkbox
问题描述
需要获取checkbox
的值,并且需要通过添加所有值而不是存储在array中的方式存储在single variable
中.我应该如何获取它?
这里是我的小提琴和我的jQuery
function getCheck() {
$('ul.addon > li :checked').each(function() {
allVals.push($(this).val());
});
return allVals;
}
$(function() {
$('ul.addon > li > input').click(getCheck);
getCheck();
$(allVals).each(function(){
addon+=parseInt(allVals);
$('#op').html(addon);
});
});
此处allvals数组包含al选中的复选框1,2,1,1,3的值
现在我需要添加这些值并移到addon
变量,例如1 + 2 + 1 + 1 + 3 = 8
如果我取消选中该复选框,则必须减去该值,我会尽力而为,无法获取.. ::(在此先感谢
如果您需要在复选框上单击以增加和减去复选框的值,请尝试下面的代码.希望这就是您的意思.
var allVals = [];
$('input:checkbox[name=addons]').each(function() {
allVals.push($(this).val());
});//Gets the complete List of values
//添加或减去点击值
var curr_value=0;
$("input:checkbox[name=addons]").on('click',function() {
if($(this).is(":checked") == false){
curr_value = curr_value -$(this).val();
}
if($(this).is(":checked") == true){
curr_value = curr_value + +$(this).val();
}
alert(curr_value);//Contains the added values of the checked boxes
});
Need to get the values of checkbox
and need to store in a single variable
by adding all values instead of storing in array.how should i get it.?
here my fiddle and my jquery
function getCheck() {
$('ul.addon > li :checked').each(function() {
allVals.push($(this).val());
});
return allVals;
}
$(function() {
$('ul.addon > li > input').click(getCheck);
getCheck();
$(allVals).each(function(){
addon+=parseInt(allVals);
$('#op').html(addon);
});
});
here allvals array contain al the values of checked checkbox 1,2,1,1,3
Now i need to add those values and move into addon
variable like 1+2+1+1+3=8
and if i uncheck the checkbox the value must be minused i tried my best i did' t able to get it.. :( thanks in advance
If you need to add and subtract the value of a checkbox on the checkbox click,try the code below.Hope this is what you meant.
var allVals = [];
$('input:checkbox[name=addons]').each(function() {
allVals.push($(this).val());
});//Gets the complete List of values
//Adds and subtracts the value on click
var curr_value=0;
$("input:checkbox[name=addons]").on('click',function() {
if($(this).is(":checked") == false){
curr_value = curr_value -$(this).val();
}
if($(this).is(":checked") == true){
curr_value = curr_value + +$(this).val();
}
alert(curr_value);//Contains the added values of the checked boxes
});
这篇关于检索复选框的值以及如何添加复选框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!