[英]Trouble with If/Else Statements in Javascript
I am working on a basic pager system for my dad's dental office. 我正在为我父亲的牙科诊所开发一个基本的寻呼机系统。 Essentially I am recreating this device, but in a much prettier touch screen form using Kindles.
本质上,我正在重新创建此设备,但是使用Kindle以更漂亮的触摸屏形式创建。 Basically, the buttons have 3 state: Off, On, Flashing.
基本上,按钮具有3种状态:关闭,打开,闪烁。 SO the idea is that I want to host a JavaScript back end that all of the Kindles are set on.
因此,我的想法是我想托管所有Kindle都已安装的JavaScript后端。 They can all send and receive updates.
他们都可以发送和接收更新。 I'm having a surprising amount of issues a creating a button that goes through these three states.
创建一个经历这三个状态的按钮时,我遇到了很多问题。
I created a basic counter that displays in a button like this. 我创建了一个基本的计数器,该计数器显示在这样的按钮中。
<body>
<script type="text/javascript">
var state = 0;
function onClick() {
state += 1;
document.getElementById("clicks").innerHTML = state;
};
</script>
<button type="button" onClick="onClick()">Click me</button>
<p>State : <a id="clicks">0</a></p>
</body></html>
But I am trying to build a function that will tell the style to loop from the 3rd state back to the first, and I didn't imagine I would have this much trouble. 但是我正在尝试构建一个函数,该函数将告诉样式从第3种状态循环回到第一种状态,而且我没有想到会有那么多麻烦。 So I tested a few variations of if/else conditions, using the assumption that that the state variable was actually holding the value as the displayed numbers were increasing.
所以我测试了if / else条件的一些变化,并假设状态变量实际上随着显示的数字增加而保持该值。 it ended up looking something like this:
最终看起来像这样:
<script>
var state = 0;
function onClick() {
if (state = 0) {
state += 1;
} else if (state = 1) {
state += 1;
} else {
state -= 1;
}
but this doesn't function... Clearly I don't get how to use conditional statements... in the first example, is the state variable not actually storing the value? 但这不起作用...显然,我不知道如何使用条件语句...在第一个示例中,状态变量是否未实际存储值? What am I doing wrong, is it syntax?
我在做什么错,语法吗?
In IF-statements, you check for equality using ==
or ===
. 在IF语句中,您使用
==
或===
检查是否相等。 A single =
is used purely for assignment. 单个
=
仅用于分配。
将=更改为== =仅用于分配,而==仅用于同盟
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.