我无法根据输入到文本字段中的数字返回某个输出

I'm having trouble returning a certain output based on the number I input into the textfield

本文关键字:数字 返回 输出 字段 文本 输入      更新时间:2023-09-26

我希望每当有人在输入文本字段中输入大于 0 的数字并点击提交按钮时,能够在div 容器中返回某个输出。我不确定我做错了什么。任何帮助将不胜感激!

.HTML

<!DOCTYPE html>
<html>
<head>
<title>Button Magic</title>
<link rel='stylesheet' type='text/css' href='Jquery.css'/>
<script type="text/javascript" src="Jquery.js"></script>
</head>
<body>
<!-- Input Field-->
<input type="text" id="first" maxlength="6" placeholder="0.00">
<!-- Span -->
<span>< Type here</span>
<!-- Submit -->
<button onclick="submitNumber()">Submit</button>
<!-- Output -->
<div id="textOutput">
Are you correct?
</div>
</body>
</html>

JAVASCRIPT

function submitNumber() {
textInput = document.getElementById('first');
textOutput = document.getElementById('textOutput');
if (textInput > 0) {
textOutput.innerHTML = 'Correct!';
} else {
textOutput.innerHTML = 'Incorrect!';
}
}

document.getElementById('first')返回一个 dom 元素,而不是输入字段的值,你需要读取输入元素的 value 属性

textInput = document.getElementById('first').value;
textOutput = document.getElementById('textOutput');

演示:小提琴

您需要

检查变量 textInput 中 DOM 元素的值,您正在检查元素 document.getElementById('first') ) 相反,您可以使用 .value 属性获取输入元素的值:

if (textInput.value > 0) {

演示