VAR设置为true /在切换功能虚假 [英] Set var to true/false in toggle function
问题描述
我有一个按钮,我想我的变量点击
设置为真正
按钮被按下时,和假
再次单击该按钮时(我想切换功能)。
I have a button and I want my variable click
to be set to true
when the button is clicked, and false
when the button is clicked again (I want toggle functionality).
在点击==真
,我希望它的输出点击
到控制台,当点击==虚假
我希望它的输出未点击
。
When click == true
, I want it to output clicked
into the console and when click == false
I want it to output not clicked
.
var click = false
下面是我的切换功能:
$('.button').toggle(
function(){
click = true;
$('.bmw').show();
}, function() {
click = false;
$('.bmw').hide();
});
和我想要它做什么:
if(click = true) {
console.log('clicked');
} else {
console.log('not clicked');
}
当我打开控制台显示未点击
的页面,但是当我preSS的按钮,它不切换到真实
。宝马对象** .bmw
然而就显示和隐藏,当我切换按钮,这似乎是工作的罚款。
When I load the page the console shows not clicked
but when I press the button it doesn't switch to true
. The bmw objects** .bmw
do however show and hide when I toggle the button so that seems to be working fine.
我在做什么错了?
推荐答案
您正在分配比较不点击的价值
You are assigning not comparing the value of click
您应该 ==
不是 =
if(click == true) {
console.log('clicked');
} else {
console.log('not clicked');
}
然而,你甚至都不需要使用如果(点击==真)
。您可以在实际上只使用如果(点击)
代替。
However you do not even need to use if(click == true)
. You can in fact just use if(click)
instead.
编辑:从我们在评论中讨论它看起来像你只有一次当code是第一次运行调用if语句,而不是每次你切换。我建议创建的if语句一个单独的函数中,然后调用切换的两个部分此方法。
From what we discussed in the comments it looks like you were calling the if statement only once when the code was first run, not everytime you toggled. I suggest creating a seperate function with the if statement inside, then calling this method in both parts of the toggle.
这篇关于VAR设置为true /在切换功能虚假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!