Javascript onsubmit="" 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8374128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:14:27  来源:igfitidea点击:

Javascript onsubmit="" not working

javascripthtmlform-submitonsubmit

提问by Howdy_McGee

I'm not sure if my error comes from submitting the form to itself via PHP but it is just not hitting my function and submits anyway no matter what is returned. I'm not sure what I'm missing here. This is my form

我不确定我的错误是否来自通过 PHP 向自身提交表单,但它只是没有命中我的函数并且无论如何都提交,无论返回什么。我不确定我在这里错过了什么。这是我的表格

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <title>Login Page</title>
    <script type="text/javascript" src="js/functions.js"></script>
    <!--Internal for simplicity or until bigger page -->
    <style type="text/css"> 
        #login div{display:inline;color:red;}
        input{margin-right:20px;}
    </style>
</head>
<body>
<form name="login" id="login" action="<?php $_SERVER['PHP_SELF'] ?>" method="POST" onsubmit="return CheckLogin()">
    <?php if($_POST && $win == false): ?>
        <div>Login Unsuccessful. Please Try Again</div><br />
    <?php endif ?>
        <input type="text" size="10" name="username" /><div></div>
            <br />
        <input type="password" size="10" name="password" /><div></div>
            <br />
    <input type="submit" text="Submit" />
</form>
<a href="register.php">Registration Page</a>
</body>
</html>

Really I'm just checking if username/password are empty but it just submits anyway going right through this function:

真的,我只是检查用户名/密码是否为空,但它只是通过此函数提交:

function CheckLogin()
{
    window.alert("made it here");
    var login = document.forms["login"];
    var user = login["username"].value;
    var pass = login["password"].value;

    if(user == null || user == "")
    {
        window.alert("user");
        return false;
    }  

    return false;
}

I'm sure it's something stupid that I'm missing so before I submitted this I double checked the function names and to see if I was getting any javascript errors but neither turned out to be the problem. Does anybody see my error?

我确定这是我遗漏的一些愚蠢的东西,所以在我提交之前,我仔细检查了函数名称,看看我是否遇到了任何 javascript 错误,但结果都不是问题。有人看到我的错误吗?

The point being I do not want the PHP to run or the form to be submitted to itself if username or password is empty or null.

关键是如果用户名或密码为空或为空,我不希望 PHP 运行或表单提交给自己。

EDITAdded full html page minus the PHP functionality. Also noted that for some reason my javascript link does not 'link' instead I can view the contents of it directly in the header like it is embedded via inspect element or firebug. This might be the root of the problem but not sure why.

编辑添加完整的 html 页面减去 PHP 功能。还注意到由于某种原因,我的 javascript 链接没有“链接”,而是我可以直接在标题中查看它的内容,就像它是通过检查元素或萤火虫嵌入的一样。这可能是问题的根源,但不确定原因。

EDITAdded doctype just incase, but still no help. It is automatically embedding my javascript into my page and putting it into CDATA like you would normally instead of actually linking it. It does this in IE, Firefox & Chrome...

编辑添加了 doctype 以防万一,但仍然没有帮助。它会自动将我的 javascript 嵌入到我的页面中,然后像通常那样将其放入 CDATA 中,而不是实际链接它。它在 IE、Firefox 和 Chrome 中执行此操作...

if anyone thinks my PHP might have something to do with it I could post it, but I don't see how server side code could interact with client side code without being posted.

如果有人认为我的 PHP 可能与它有关,我可以发布它,但我看不到服务器端代码如何在不发布的情况下与客户端代码进行交互。

回答by Savas Vedova

There are some minor errors in your code. First of all there is no attribute called "text" for an input, I believe that you missed it :) Try to validate them first. Check hereto see the attributes that an input may take. Second of all, this jsfiddleexample works properly in Chrome and FF. I cannot test in IE since I am using Ubuntu.

您的代码中有一些小错误。首先,输入没有称为“文本”的属性,我相信您错过了它:) 尝试先验证它们。检查此处以查看输入可能具有的属性。其次,这个jsfiddle示例在 Chrome 和 FF 中正常工作。我无法在 IE 中进行测试,因为我使用的是 Ubuntu。

After I corrected the form attributes, the example in jsfiddle started to work. I am not sure whethere it's related with that, but apparently that seems the only logical explanation to me for your code not working.

在我更正表单属性后,jsfiddle 中的示例开始工作。我不确定它是否与此有关,但显然这似乎是我对您的代码不起作用的唯一合乎逻辑的解释。

回答by Shanshui

Your code runs fine. Try running it in another web browser. It's possible that one of the extensions you installed on your browser is causing a JS error (having Google Dictionary on Chrome was causing issues for me).

您的代码运行良好。尝试在另一个 Web 浏览器中运行它。您在浏览器上安装的其中一个扩展程序可能导致 JS 错误(Chrome 上的 Google 词典给我带来了问题)。