Javascript if语句上的变量未更改
我正在为一个网站编写代码,我试图获取一个输入,并根据输入显示一条特定的消息。无论何时我输入一个数字,它都会被设置为第一个if结果。任何帮助都会很好,谢谢Javascript if语句上的变量未更改,javascript,Javascript,我正在为一个网站编写代码,我试图获取一个输入,并根据输入显示一条特定的消息。无论何时我输入一个数字,它都会被设置为第一个if结果。任何帮助都会很好,谢谢 <form name="testsDone"> How many of your tests have you taken? <input type ="text" id="tookTests" /><br /> </form> <script> function t
<form name="testsDone">
How many of your tests have you taken? <input type ="text" id="tookTests" /><br />
</form>
<script>
function testCheck(){
var a = parseFloat(document.getElementById("tookTests").value);
var b = a;
var c;
if (b = 2 ) {
c = "Yes you can! You've earned it.";
} else if (b = 1) {
c = "You need to keep studying then";
}
else if (b > 2 ) {
c = "That's more tests than you're supposed to have";
}else {
c = "Why are you on here? You need to study.";
}
document.getElementById("change").innerHTML = c;
}
</script>
<button onclick="testCheck()">Check and See</button>
<p> Can you go to sleep?</p>
<p id="change"></p>
你参加了多少次考试
函数testCheck(){
var a=parseFloat(document.getElementById(“tookTests”).value);
var b=a;
var c;
如果(b=2){
c=“是的,你可以!这是你应得的。”;
}否则如果(b=1){
c=“你需要继续学习”;
}
否则,如果(b>2){
c=“这比你应该做的更多”;
}否则{
c=“你为什么在这里?你需要学习。”;
}
document.getElementById(“change”).innerHTML=c;
}
查查
你能去睡觉吗
您正在此处赋值
如果(b=2){
应该是
如果(b==2){他们将其更改为以下内容
function testCheck(){
var a = parseFloat(document.getElementById("tookTests").value);
var b = a;
var c;
if (b == 2 ) {
c = "Yes you can! You've earned it.";
} else if (b == 1) {
c = "You need to keep studying then";
}
else if (b > 2 ) {
c = "That's more tests than you're supposed to have";
}else {
c = "Why are you on here? You need to study.";
}
document.getElementById("change").innerHTML = c;
}
相等是
=
或==
,而不是=
=是分配,==是比较=
用于比较,=
用于分配为什么不投票!?