这看起来非常微不足道,但我不知道它为什么不工作。JavaScript布尔真正变成假,假不变真
我正在使用div而不是我的web应用程序的复选框。我正在使用名为“contentEditable”的JavaScript功能将布尔属性设置为HTML div元素。
下面是切换属性true或false代码:
$(document).on('click', '.chartPageSensorBox', function() {
var chartNumber = this.id.charAt(6),
visibilityIndex = this.id.charAt(9),
checkBoxToggle = this.id.contentEditable;
//the alert below returns default value, output is: 'true true'
alert(this.id.contentEditable + " " + checkBoxToggle);
checkBoxToggle = !checkBoxToggle;
this.id.contentEditable = checkBoxToggle;
//the alert output now is: 'false false'
alert(this.id.contentEditable + " " + checkBoxToggle);
//The series for the chart successfully turns off because it is false
allCharts[chartNumber - 1].dyGraph.setVisibility(visibilityIndex, checkBoxToggle);
});
现在的问题是,当我再次点击股利,虚假仍假永不变回真,我不理解为什么自if语句应该将checkBoxToggle变为true。编辑#1:清理代码,但没有修复错误只是为了让未来的读者更清晰。
编辑#2:请参阅下面的代码。核心问题仍然存在......
$(document).on('click', '.chartPageSensorBox', function() {
var chartNumber = this.id.charAt(6),
visibilityIndex = this.id.charAt(9),
checkBoxToggle = $(this).prop("contentEditable");
alert(checkBoxToggle);
checkBoxToggle = !checkBoxToggle;
alert(checkBoxToggle);
//After the alerts, true becomes false, but false remains false indefinitely.
$(this).prop("contentEditable", checkBoxToggle);
allCharts[chartNumber - 1].dyGraph.setVisibility(visibilityIndex, checkBoxToggle);
});
'的document.getElementById(this.id)'?为什么不使用'this'? –
愚蠢的错误...好点。 – NutellaAddict
我知道。这就是我最初做的......我'扩大'了代码以尝试调试它。 – NutellaAddict