Boolean(“false") 返回 true .. 任何替代方法? [英] Boolean("false") returns true.. any alternative?
本文介绍了Boolean(“false") 返回 true .. 任何替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 jquery 将真假字符串写入数据-html 属性.但是,当我将 false 写入其中一个属性,然后将其检查为 false 时,它返回 true.我也在网上读到它应该这样做,这完全没有意义.
I am using jquery to write true and false strings to data- html attributes. However when I write false to one of the attributes, and then check it it false, it returns true. I also read online that it supposed to do that, which really makes no sense at all.
有没有人有替代品?任何帮助将非常感激!
Does anyone have an alternative? Any help would be really appreciated!
(function() {
// $(".opt-header").click(function () {
$(document).on("click", ".opt-header", function() {
console.log($(this).attr("data-is-panel-activated"));
var is_activate = Boolean($(this).attr("data-is-panel-activated"));
$(this).siblings(":not(.opt-group-toggle)").slideToggle(300);
console.log(is_activate);
if (is_activate) {
$(this).find(".fa-angle-right").replaceWith("<i class='fa fa-angle-down'></i>");
$(this).attr("data-is-panel-activated", "false");
} else {
$(this).attr("data-is-panel-activated", "true");
$(this).find(".fa-angle-down").replaceWith("<i class='fa fa-angle-right'></i>");
}
})
})();
推荐答案
Boolean('false')
将返回 true
因为字符串 'false'
是真的.
Boolean('false')
will return true
because the string 'false'
is truthy.
您可以运行检查以查看字符串是否等于false":
You could just run a check to see if the string equals 'false':
if ($(this).attr("data-is-panel-activated") === 'false') {
.....
}
这篇关于Boolean(“false") 返回 true .. 任何替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文