简体   繁体   English

HTML onclick不会触发功能

[英]HTML onclick doesn't trigger function

<html>
<title>Register</title>
<head>
<script type="text/javascript" src="sha512.js"></script>
<script>
function formhash(form, password, confirm_password) {
    if(password.value==confirm_password.value){
        var p = document.createElement("input");
        p.name = "p";
        p.type = "hidden";
        p.value = hex_sha512(password.value);
        password.value = "";
        form.appendChild(p);    
        form.submit;
        return true;
    }
    else{
        header('Location: ./register.php?error=1');
        return false;
    }
}
</script>
</head>
<body>
<form action="process_register.php" method="post" name="register_form">
    Username: <input type="text" name="username"/></br>
    Email: <input type="text" name="email"/></br>
    Password: <input type="password" id="password" name="password"/></br>
    Confirm Password: <input type="password" id="confirm_password" name="confirm_password"/></br>
    <input type="submit" value="Register" onclick="return formhash(this.form, this.form.password, this.form.confirm_password);"/>
</form>
</body>
</html>

Hi all, I'm relatively new to web coding. 大家好,我对网络编码比较陌生。 I'm trying to do a registration page, but the onclick doesn't trigger the formhash function at all. 我正在尝试进行注册页面,但onclick根本不会触发formhash功能。 Can anybody enlighten me? 任何人都可以开导我吗? Thanks! 谢谢!

Don't use inline event listeners. 不要使用内联事件侦听器。 This is what I have learned over the past months. 这是我过去几个月所学到的。

Try these: 试试这些:

  1. Give the Submit button an ID (id ="sumitbbutton") or whatever. 给提交按钮一个ID(id =“sumitbbutton”)或其他。
  2. Use this code to check for clicks: document.getElementById("submitbutton").onclick = thyfunction() 使用此代码检查点击次数:document.getElementById(“submitbutton”)。onclick = thyfunction()

Check the JS console (hit f12 in chrome) and see if any errors are being thrown, and post them here. 检查JS控制台(在chrome中点击f12)并查看是否有任何错误被抛出,并在此处发布。

The "return" may also be messing up the code. “回归”也可能搞乱了代码。

I'm not the best at Js, but that's all I know. 我不是Js最好的,但这就是我所知道的。 Hope it helps :) 希望能帮助到你 :)

PS Try Checking @Robbert 's comment. PS试试看@Robbert的评论。

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

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