问题描述
我正在使用 Notepad++ 创建一个简单的网页,其中用户在文本框中输入两个数字,然后按下按钮.当他们按下按钮时,会出现一些信息,告诉他们第一个或第二个数字是否更大.我有以下代码,但无法提供任何内容.有谁知道出了什么问题?
I am using Notepad++ to create a simple web page where a user types in two numbers into a text box, and then presses a button. When they press the button something comes up that tells them whether the first or second number is greater. I have the following code but cant get anything to come up. Does anyone know whats wrong?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Assignment 10 Form</title>
<script type="text/javascript">
function greaterNum(){
var value1;
var value2;
value1 = document.First_num.value;
value2 = document.last_num.value;
if (value1 > value2){
alert('Value 1 is greater than value 2');
document.body.style.background = "orange";
}
}
</script>
<style type="text/css">
body{background-color: #40FF00;
margin-left: auto;
margin-right: auto;
width: 60%;
}
#container{
border: 2px solid yellow;
padding: 20px;
}
</style>
</head>
<body>
<h1>Assignment 10</h1>
<div id="container">
<div class="Num">
<form>
<label class="label1">Enter Value 1:</label>
<input type="text" name="First_num" value=" " />
<label class="label1">Enter Value 2:</label>
<input type="text" name="last_num" value=" " />
<br/>
<input type="button" value=" Which number is greater? " onclick="greaterNum();" />
</form>
</body>
</html>
推荐答案
来自输入的值作为字符串检索,您需要将其转换为数字.试试这个:
Values from inputs are retrieved as strings, you need to convert it to number. Try this:
value1 = +document.First_num.value;
value2 = +document.last_num.value;
另外,在命名输入时尽量保持一致,为什么一个大写而另一个小写?
Also, try to be consistent when naming your input, why caps for one and lowercase for the other?
这篇关于使用 Javascript 比较 HTML 中的两个输入数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!