gpt4 book ai didi

javascript - 从 html 输入中获取值

转载 作者:行者123 更新时间:2023-12-03 04:26:46 24 4
gpt4 key购买 nike

我试图从 html 输入中获取值,但我无法使其正常工作,从我自己的研究来看,我的语法似乎正确?所以我很困惑。

当我控制台记录该值时,看起来它实际上并未获取输入的值。

let btn = document.getElementById("btn");
let grade = document.getElementById("gradeNum").value;

function getGrade() {
console.log(grade);

if (grade >= 90) {
document.getElementById("gradeLetter").innerHTML = "You got an A!";
} else if (grade < 90 && grade >= 80) {
document.getElementById("gradeLetter").innerHTML = "You got a B!";
} else if (grade < 80 && grade >= 70) {
document.getElementById("gradeLetter").innerHTML = "You got a C!";
} else if (grade < 70) {
document.getElementById("gradeLetter").innerHTML = "You got an F! Study more!";
}
};

最佳答案

试试这个:

let btn = document.getElementById("btn");
// let grade = document.getElementById("gradeNum").value; <-- NOT HERE

function getGrade() {
let grade = document.getElementById("gradeNum").value; // BUT HERE
console.log(grade);

if (grade >= 90) {
...

关于javascript - 从 html 输入中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43672633/

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