简体   繁体   English

JavaScript:Form onSubmit不调用函数

[英]JavaScript: Form onSubmit not calling function

I am currently learning JavaScript for my uni course and my function is not being called for some reason. 我目前正在为单门课程学习JavaScript,由于某种原因我的函数没有被调用。 I want to understand why this is not working. 我想了解为什么这不起作用。

Brief Description 简要描述;简介

There are two forms, one has a submit button, with the onSubmit field calling document.write() (This works), then calling my own function submit() . 有两种形式,一种具有提交按钮, onSubmit字段调用document.write() (此方法有效),然后调用我自己的函数submit() If I change submit() to document.write() then I receive two outputs, so there should be no reads why submit is not called? 如果我将submit()更改为document.write()那么我会收到两个输出,因此应该没有读取内容,为什么不调用submit

The second form has a single textbox in it, I ideally want to make this disappear when the button is pressed, however I mainly want to understand why submit is not called. 第二种形式中只有一个文本框,理想情况下是希望在按下按钮时使它消失,但是我主要想了解为什么未调用submit

<html charset="utf-8">
<head>
    <title>Game</title>
</head>
<body>
    <form method="get" name="form1" action="game.php" onSubmit="document.write('Hello');submit();">
        <input type="submit" value="submit" name="button" />
    </form>

    <form id="form2" name="form2">
        <input name="letter" type="text" />
    </form>
    <script>
        function submit() {
            alert("HELLO");
            document.getElementById('form2').visibility='hidden';
            document.write("Hello");
        }
    </script>
</body>

I have tried inserting the script in the header, above the function, below the function, but nothing seems to work. 我尝试将脚本插入标头,函数上方,函数下方,但似乎无济于事。

Hope someone can help, thanks 希望有人可以帮助,谢谢

submit is a reserved keyword. 提交是保留关键字。 Change to any other name and it should work. 更改为任何其他名称,它应该起作用。 Please find the JSFiddle of the same 请找到相同的JSFiddle

https://jsfiddle.net/pc9rL2ey/ https://jsfiddle.net/pc9rL2ey/

        function submita() {
            alert("HELLO");
            document.getElementById('form2').visibility='hidden';
            document.write("Hello");
        }


<form method="get" name="form1" action="game.php" onSubmit="submita();document.write('Hello');">

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

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