使用Javascript比较HTML中的两个输入数字? [英] Using Javascript to compare two input numbers in HTML?

查看:117
本文介绍了使用Javascript比较HTML中的两个输入数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Notepad ++创建一个简单的网页,用户在两个数字中键入一个文本框,然后按下一个按钮。当他们按下按钮时会出现一些信息,告诉他们第一个或第二个数字是否更大。我有下面的代码,但不能得到任何东西来。有谁知道最新错误?


$ b $

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Strict // EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
< head>
< meta http-equiv =content-typecontent =text / html; charset = utf-8/>
< title>作业10表格< /标题>

< script type =text / javascript>

函数greaterNum(){
var value1;
var value2;
value1 = document.First_num.value;
value2 = document.last_num.value;
if(value1> value2){
alert('值1大于值2');
document.body.style.background =orange;
}

}


< / script>


< style type =text / css>
body {background-color:#40FF00;
margin-left:auto;
margin-right:auto;
宽度:60%;
}

#container {
border:2px纯黄色;
padding:20px;
}

< / style>

< / head>

< body>
< h1>作业10< / h1>

< div id =container>
< div class =Num>
< form>
< label class =label1>输入值1:< / label>
< input type =textname =First_numvalue =/>
< label class =label1>输入值2:< / label>
< input type =textname =last_numvalue =/>
< br />
< input type =buttonvalue =哪个数字更大?onclick =greaterNum(); />
< / form>

< / body>
< / html>


解决方案

来自输入的值被检索为字符串,您需要将其转换为数字。试试这个:

  value1 = + document.First_num.value; 
value2 = + document.last_num.value;

另外,为输入命名时要保持一致性,为什么要为一个和另一个小写字母上限?

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中的两个输入数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆