[英]HTML form which takes the input and has an if function
I know this is a basic thing but just starting out guys. 我知道这是基本的事情,但是刚开始是伙计们。
So I need to create a form with a set page title, a set main heading, a form input field and enter button. 因此,我需要创建一个具有设置页面标题,设置主标题,表单输入字段和输入按钮的表单。 When the enter button is clicked the programme has to inspect the age entered into the input field and display one message if under 17 and another message if 17 or over.
单击回车按钮时,程序必须检查在输入字段中输入的年龄,如果未满17岁,则显示一条消息,如果未满17岁,则显示另一条消息。
Here is my code - the if function just isn't working - any ideas? 这是我的代码-if函数不起作用-有什么想法吗?
<html>
<head>
<title>Name of set title</title>
<script language="javaScript">
function AgeLimits()
{
if(yourage>=17) document.write("You are old enough to drive");
else document.write("You are too young to drive");
}
</script>
</head>
<body>
<h1>Name of set question</h1>
<p>Please enter your age below</p>
<hr/>
<form name="Name of set question">
<p><input type="text"length="3"name="yourage"></p>
<p><input type="button"value="Enter"onClick="AgeLimits();"></p>
<hr/>
</form>
</body>
</html>
You need to select the element to get the value keyed in by the user: 您需要选择元素以获取用户键入的值:
<html>
<head>
<title>Name of set title</title>
<script language="javascript">
function AgeLimits()
{
var yourage = document.getElementById("yourage").value;
if (yourage >= 17)
alert("You are old enough to drive");
else
alert("You are too young to drive");
}
</script>
</head>
<body>
<h1>Name of set question</h1>
<p>Please enter your age below</p>
<hr />
<form method="post">
<p><input type="text" length="3" id="yourage"></p>
<p><input type="button" value="Enter" onclick="AgeLimits()"></p>
</form>
<hr />
</body>
</html>
This is not a way to get text entered in textfield. 这不是在文本字段中输入文本的方法。 The right way is document.getElrmentByName('yourage').value.
正确的方法是document.getElrmentByName('yourage')。value。 Since u are checking value of a variable which is not defined your condition always executes else part
由于u正在检查未定义的变量的值,因此您的条件将始终执行else部分
The Function should be like this 函数应该是这样的
function AgeLimits()
{
var yourage=document.getElementById("age").value;
if(yourage>18)
window.alert("You are eligible to vote");
else
window.alert("You are not eligible.Sorry")
}
Add a attribute id="age"
to the text input ! 在文本输入中添加一个属性
id="age"
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.