gpt4 book ai didi

javascript - 基于两个输入的输出

转载 作者:行者123 更新时间:2023-12-03 01:33:38 25 4
gpt4 key购买 nike

我正在尝试制作这个“我应该参加这个测试还是那个测试”计算器。当我输入这两个值时,没有任何输出,因此我正在查找代码中的错误。谢谢!

document.getElementById("test2score").onkeyup = function() {
WhichTestFunction()
};

function WhichTestFunction() {
var t1 = document.getElementById('test1score').value;
var t2 = document.getElementById('test2score').value;

if (t1 == 9) && (t2 < 120) {
score = "Take Test 1";
}

else if (t1 == 9) && (t2 > 140) {
score = "Take Test 2";
}

else if (act == 9) && (sat >= 120) && (sat <= 140) {
score = "Toss up. Take the test you like better based on format and content.";
}

document.getElementById("testscore").innerHTML = score;
}
<section id="" class="">
<div class="container text-center">
<div class="col-md-6 col-md-offset-1">
<h2><b>Which Test To Take?</b></h2>
<br>
<div class="col-md-4 col-md-offset-1">
<h6><b>Input Test 1 Score</b></h6>
<input type="number" id="test1score">
</div>
<div class="col-md-4 col-md-offset-1">
<h6><b>Input Test 2 Score</b></h6>
<input type="number" id="test2score">
</div>
<p id="testscore"></p>
</div>
</div>
</section>

最佳答案

首先,您需要使用外括号将 if 语句括起来

if ((act == 9) && (sat >= 120) && (sat <= 140)) {...}

然后有几个未声明的变量导致异常

var score, sat, act;

注释 1a:我不知道这 3 个变量是否应该在全局范围内声明/可用。

注释 1b:由于不知道它们实际上应该是什么,我注释掉了最后一个 if 语句,以便代码将在此演示示例中运行。

堆栈片段

<section id="" class="">
<div class="container text-center">
<div class="col-md-6 col-md-offset-1">
<h2><b>Which Test To Take?</b></h2>
<div class="col-md-4 col-md-offset-1">
<h6><b>Input Test 1 Score</b></h6>
<input type="number" id="test1score">
</div>
<div class="col-md-4 col-md-offset-1">
<h6><b>Input Test 2 Score</b></h6>
<input type="number" id="test2score">
</div>
<p id="testscore"></p>
</div>
</div>
</section>

<script>

document.getElementById("test2score").onkeyup = function() {WhichTestFunction()};

var score, sat, act;

function WhichTestFunction() {
var t1 = document.getElementById('test1score').value;
var t2 = document.getElementById('test2score').value;

if ((t1 == 9) && (t2 < 120)) {
score = "Take Test 1";
}

else if ((t1 == 9) && (t2 > 140)) {
score = "Take Test 2";
}
/*
else if ((act == 9) && (sat >= 120) && (sat <= 140)) {
score = "Toss up. Take the test you like better based on format and content.";
}
*/
document.getElementById("testscore").innerHTML = score;
}


</script>

关于javascript - 基于两个输入的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51177268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com