Javascript 基于复选框添加数字
所以我有3个布尔值来表示3个复选框,我的想法是我需要一个函数来添加一些值,根据选中的复选框,当我console.log(sum)时,我什么也得不到Javascript 基于复选框添加数字,javascript,vue.js,Javascript,Vue.js,所以我有3个布尔值来表示3个复选框,我的想法是我需要一个函数来添加一些值,根据选中的复选框,当我console.log(sum)时,我什么也得不到 calculateNumber() { const sum = 0 if (this.ruleForm.ownerBoolean === true) { return sum + 1 } if (this.ruleForm.agentBoolean === true) { return sum + 2 }
calculateNumber() {
const sum = 0
if (this.ruleForm.ownerBoolean === true) { return sum + 1 }
if (this.ruleForm.agentBoolean === true) { return sum + 2 }
if (this.ruleForm.operatorBoolean === true) { return sum + 4 }
console.log(sum)
}
如果第一个复选框被选中添加1,如果第二个复选框被选中添加2,如果第三个复选框被选中添加4,我需要最后的值来求和,我做错了什么?尝试以下方法:
calculateNumber() {
let sum = 0
if (this.ruleForm.ownerBoolean === true) { sum += 1 }
if (this.ruleForm.agentBoolean === true) { sum += 2 }
if (this.ruleForm.operatorBoolean === true) { sum += 4 }
console.log(sum)
return sum
}
试试这个:
calculateNumber() {
let sum = 0
if (this.ruleForm.ownerBoolean === true) { sum += 1 }
if (this.ruleForm.agentBoolean === true) { sum += 2 }
if (this.ruleForm.operatorBoolean === true) { sum += 4 }
console.log(sum)
return sum
}
在你回来之前。首先计算
sum
变量的值,并在方法末尾返回
calculateNumber() {
let sum = 0
if (this.ruleForm.ownerBoolean === true) sum++
if (this.ruleForm.agentBoolean === true) sum += 2
if (this.ruleForm.operatorBoolean === true) sum += 4
console.log(sum)
return sum
}
在你回来之前。首先计算
sum
变量的值,并在方法末尾返回
calculateNumber() {
let sum = 0
if (this.ruleForm.ownerBoolean === true) sum++
if (this.ruleForm.agentBoolean === true) sum += 2
if (this.ruleForm.operatorBoolean === true) sum += 4
console.log(sum)
return sum
}
我猜你想根据布尔值求和 在这种情况下,您需要删除
返回值
,而是按如下方式递增总和:
calculateNumber() {
let sum = 0
if( this.ruleForm.ownerBoolean === true ) { sum+= 1; }
if( this.ruleForm.agentBoolean === true ) { sum+= 2; }
if( this.ruleForm.operatorBoolean === true) { sum+= 4; }
console.log(sum)
}
在
控制台.log
调用之后添加return sum
可能也是一个好主意,这样您以后就可以实际使用该值了。我猜您希望根据布尔值实际求和sum
在这种情况下,您需要删除返回值
,而是按如下方式递增总和:
calculateNumber() {
let sum = 0
if( this.ruleForm.ownerBoolean === true ) { sum+= 1; }
if( this.ruleForm.agentBoolean === true ) { sum+= 2; }
if( this.ruleForm.operatorBoolean === true) { sum+= 4; }
console.log(sum)
}
在控制台.log
调用之后添加返回和
可能也是一个好主意,这样您以后可以实际使用该值。从中,return语句结束函数执行并指定要返回给函数调用方的值。比如说,
........(some other code here)
var a = returnSomething();
function returnSomething() {
if(b) { return 1; } //if b is true, next 2 lines will never execute and variable a will be assigned number 1
if(c) { return 2; } //if a is true, next line will never execute and variable a will be assigned number 2
console.log("this line will only print if both b and c are false.");
}
如果没有return语句,默认情况下将返回undefined
。在函数体中使用return语句时,函数的执行将停止。所以,有时我们只使用return
停止函数其余部分的执行
现在,关于calculateNumber
函数,您将返回sum+1
和其他变量,但您没有向变量sum
添加任何内容。所以不管怎样,sum
的值总是0。您必须添加sum=sum+1(或2/或4等),而不是返回。所以你的函数应该是这样的:
calculateNumber() {
let sum = 0;
if (this.ruleForm.ownerBoolean === true) { sum += 1 }
if (this.ruleForm.agentBoolean === true) { sum += 2 }
if (this.ruleForm.operatorBoolean === true) { sum += 4 }
console.log(sum);
}
希望这有助于……:)
编辑:刚才注意到您在声明sum
时使用了const
。在这种情况下,这完全是错误的,因为const
是常量的缩写,您不能更改常量的值。因此,可以使用let
或var
。from,return语句结束函数执行,并指定要返回给函数调用方的值。比如说,
........(some other code here)
var a = returnSomething();
function returnSomething() {
if(b) { return 1; } //if b is true, next 2 lines will never execute and variable a will be assigned number 1
if(c) { return 2; } //if a is true, next line will never execute and variable a will be assigned number 2
console.log("this line will only print if both b and c are false.");
}
如果没有return语句,默认情况下将返回undefined
。在函数体中使用return语句时,函数的执行将停止。所以,有时我们只使用return
停止函数其余部分的执行
现在,关于calculateNumber
函数,您将返回sum+1
和其他变量,但您没有向变量sum
添加任何内容。所以不管怎样,sum
的值总是0。您必须添加sum=sum+1(或2/或4等),而不是返回。所以你的函数应该是这样的:
calculateNumber() {
let sum = 0;
if (this.ruleForm.ownerBoolean === true) { sum += 1 }
if (this.ruleForm.agentBoolean === true) { sum += 2 }
if (this.ruleForm.operatorBoolean === true) { sum += 4 }
console.log(sum);
}
希望这有助于……:)
编辑:刚才注意到您在声明
sum
时使用了const
。在这种情况下,这完全是错误的,因为const
是常量的缩写,您不能更改常量的值。因此,要么使用let
或者var
。我已经为此编写了一个简单的提琴。遵循此方法来实现您的需求
HTML
var demo=新的Vue({
el:'演示',
数据:{
总数:0,,
主要类别:[{
id:1,
名称:“ownerBoolean”
}, {
id:2,
名称:“代理工具”
}, {
id:4,
名称:“operatorBoolean”
}
]//使用数据进行测试:[{done:false,内容:'testing'}]
},
方法:{
检查:功能(e){
如果(例如,目标已选中){
this.sum=parseInt(this.sum)+parseInt(e.target.value);
}
否则{
this.sum=parseInt(this.sum)-parseInt(e.target.value);
}
}
}
})
正文{
字体系列:Helvetica Neue,Arial,无衬线;
}
李:好了{
文字装饰:线条贯通;
}
[v-斗篷]{
显示:无;
}
SUM{{SUM}
-
{{mainCat.name}
我为此编写了一个简单的小提琴。遵循此方法来实现您的需求
HTML
var demo=新的Vue({
el:'演示',
数据:{
总数:0,,
主要类别:[{
id:1,
名称:“ownerBoolean”
}, {
id:2,
名称:“代理工具”
}, {
id:4,
名称:“operatorBoolean”
}
]//使用数据进行测试:[{done:false,内容:'testing'}]
},
方法:{
检查:功能(e){
如果(例如,目标已选中){
this.sum=parseInt(this.sum)+parseInt(e.target.value);
}
否则{
this.sum=parseInt(this.sum)-parseInt(e.target.value);
}
}
}
})
正文{
字体系列:Helvetica Neue,Arial,无衬线;
}
李:好了{
文字装饰:线条贯通;
}
[v-斗篷]{
显示:无;
}
SUM{{SUM}
-
{{mainCat.name}
如果任何布尔值是真的,那么您的控制台.log将不会执行。另外,声明sum
有什么意义?您也可以分别添加return 1
、return 2
和return 4
。K,但这个想法至少是好的?也许,您可以去掉==true
。如果任何布尔值都是truthy,您的控制台.log
将不会执行。另外,声明sum
有什么意义?您不妨分别添加return1
、return2
和return4
。K,但这个想法至少是好的?可能是