简体   繁体   English

表单提交后如何更改html文本?

[英]How do you change a html text after form submission?

I am trying to create a form and based on the inputs of the form, I want to give out a result.我正在尝试创建一个表单,并根据表单的输入,我想给出一个结果。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="design.css">
        <title>Plan <B></B></title>
    </head>

    <body>
        <h1>Plan B</h1>
        <form onsubmit="return processInputs()">
            <label for="tnumber">Team Number:</label>
            <input type="text" id="tnumber" name="tnumber"><br><br>
            <input type="submit" value="Submit">
        </form>
        <p>Results:</p>
        <p id="results">something</p>
    </body>

    <script type="text/javascript">
        //Processing Inputs
        function processInputs(){
            document.getElementById("results").innerHTML = "77%";
        }
    </script>

</html>

Above, I am obtaining inputs from the data by making a form attribute.上面,我通过制作表单属性从数据中获取输入。 When the user submits the form, I want the text of the "id=results" (in this case, it is "something") to change.当用户提交表单时,我希望“id=results”(在这种情况下,它是“某物”)的文本发生变化。 So for now, I wanted that text to change to "77%".所以现在,我希望该文本更改为“77%”。

However, upon the submission of the form, the code does not seem to work.但是,提交表单后,代码似乎不起作用。 Are there any ways of fixing this code?有没有办法修复这个代码?

Thanks.谢谢。

Add return false to the processInputs() functionreturn false添加到processInputs()函数

 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="design.css"> <title>Plan <B></B></title> </head> <body> <h1>Plan B</h1> <form onsubmit="return processInputs()"> <label for="tnumber">Team Number:</label> <input type="text" id="tnumber" name="tnumber"><br><br> <input type="submit" value="Submit"> </form> <p>Results:</p> <p id="results">something</p> </body> <script type="text/javascript"> //Processing Inputs function processInputs(){ document.getElementById("results").innerHTML = "77%"; return false; } </script> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM