[英]JQuery toggle doesn't work on change when I try to set css
I have a toggle button that works only once.我有一个只能使用一次的切换按钮。 This is the portion of script:这是脚本的一部分:
$('#toggle').change(function(){
var mode= $(this).prop('unchecked');
if(mode = true) {
$('.container').css('display','block');
$('.container').hide().delay(500).fadeIn(1000);
$('#map').css('filter','grayscale(100%)');
} else {
$('.container').css('display','none');
$('.container').delay(500).fadeOut(1000);
$('#map').css('filter','grayscale(0%)');
}
});
The first part of If condition is fired, not the Else part. If 条件的第一部分被触发,而不是 Else 部分。 I don't understand why.我不明白为什么。 Some help?一些帮助?
There are two errors in you code.您的代码中有两个错误。 1) There is no such property as:unchecked, you should use:checked property 2) You have written if(mode = true) not if(mode == true), so you didn't check equality, you assign new value 1)没有这样的属性:unchecked,你应该使用:checked属性2)你写了if(mode = true)而不是if(mode == true),所以你没有检查相等性,你分配新值
There is right code:有正确的代码:
$('#toggle').change(function(){
var mode= $(this).prop('checked');
console.log(mode);
if(mode == true) {
$('.container').css('display','block');
$('.container').hide().delay(500).fadeIn(1000);
$('#map').css('filter','grayscale(100%)');
} else {
$('.container').css('display','none');
$('.container').delay(500).fadeOut(1000);
$('#map').css('filter','grayscale(0%)');
}
});
A couple issues here.这里有几个问题。 @AidOnline01 mentioned the first two. @AidOnline01 提到了前两个。
unchecked
doesn't exist, use checked
instead.属性unchecked
不存在,使用checked
代替。if (mode = true)
statement assigns mode
a value instead of evaluating equality. if (mode = true)
语句为mode
分配一个值,而不是评估相等性。 Use if (mode == true)
使用if (mode == true)
$('.container').css('display','none');
使用回调调用$('.container').css('display','none');
I've made a JSFiddle to demonstrate: https://jsfiddle.net/g3y27o5L/4/我制作了一个 JSFiddle 来演示: https://jsfiddle.net/g3y27o5L/4/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.