简体   繁体   English

接受输入并具有if函数的HTML表单

[英]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.

相关问题 测试从 HTML 输入元素获取值的函数的最佳方法是什么? - The best way in which to test a function that takes value from HTML input element? 如何对用户以 html 形式输入的数据应用字符串化? - How can i apply stringify on the data which has been input by the user in an html form? 编写一个函数,将一个句子作为输入并输出一个排序后的句子 - Write a function which takes a sentence as an input and output a sorted sentence 我希望只有在验证HTML5表单输入后才能运行javascript函数 - I want to have a javascript function run only after HTML5 form input has been validated 在表单提交后和HTML5验证表单之后,是否有我可以监听的事件? - Is there an event I can listent to that takes place after a form submit and after the form has been verified with HTML5? Html 表单输入变量用于 js function - Html form input to variable for use in a js function HTML表单输入无效,然后使用javascript函数 - HTML Form input invalid then javascript function HTML表单输入值未在javascript函数中更新 - HTML Form input value not updating in javascript function 使用JavaScript或jQuery检测哪个表单输入具有焦点 - Detect which form input has focus using JavaScript or jQuery 创建采用rgb颜色值的HTML输入字段,该值会更改javascript中的textarea背景 - Create HTML input field that takes an rgb colour value which changes a textarea background in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM