简体   繁体   English

防止按钮表单提交Javascript

[英]prevent button form from submission Javascript

This form keeps on submitting even if it executes the return value, what is the problem with my code? 即使执行返回值,此表单也会继续提交,我的代码有什么问题?

function formhash (form, password)
{
    var pass1 = document.getElementById("password").value;
    var pass2 = document.getElementById("cpassword").value;
    var ok = true;
    if (password != cpassword) {
        //alert("Passwords Do not match");
        document.getElementById("password").style.borderColor = "#E34234";
        document.getElementById("cpassword").style.borderColor = "#E34234";
        ok = false;
        return;
    }
    else
    {
        $.post('insert_home.php'
            {PRIMAID:PRIMAID,EDITCAP:EDITCAP,EDITIMG:EDITIMG,EB_TITLE:EB_TITLE}).done(function(data){
            alert ("Book Successfully Updated");
            location.reload();
        });


        var p = document.createElement("input");    
        form.appendChild(p);
        p.name="p";
        p.type="hidden";
        p.value=hex_sha512(password.value);
        password.value="";
        form.submit();
    }
}

You are calling form.submit(); 您正在调用form.submit(); , remove it and it won't submit. ,将其删除,它将不会提交。

You are using the wrong variable names "password" and cpassword". You created pass1 and pass2 so you need to use those. 您使用了错误的变量名“ password”和“ cpassword”。您创建了pass1pass2因此需要使用它们。

Change to this: 更改为此:

//You were using the WRONG variable names
if (pass1 != pass2) {
    //alert("Passwords Do not match");
    document.getElementById("password").style.borderColor = "#E34234";
    document.getElementById("cpassword").style.borderColor = "#E34234";
    ok = false;
    return false;
}

I don't see where formhash() is used. 我看不到在哪里使用formhash()。

The code calls the submit, not the function. 该代码调用提交,而不是函数。

If it submits the form, it' because it's falling in the else condition. 如果提交表单,那是因为它处于else条件。

Add lots of "console.log("debug_X")" where "X" is a number different each time. 添加很多“ console.log(“ debug_X”)“,其中“ X”每次都是不同的数字。

You have to add inside a console.log the value of pass1 and pass2. 您必须在console.log内部添加pass1和pass2的值。

Maybe you are passign a value instead of an object or maybe the reversed situation can happen also. 也许您是用值代替对象,或者也可能发生相反的情况。

My instinct tells me to check both password object, and their values. 我的直觉告诉我要同时检查密码对象及其值。 Also it tells me that you didn't check the content of pass1 and pass2 before posting a comment to Don Rhummy. 它还告诉我,在向Don Rhummy发表评论之前,您没有检查pass1和pass2的内容。

Check carefully and please search more before posting your next answer. 请仔细检查,并在发布下一个答案之前进行更多搜索。

You can have the value by doing "console.log(pass1);" 您可以通过执行“ console.log(pass1);”获得值。 and by opening firebug and by checking the javascript console. 并打开Firebug并检查javascript控制台。

从窗体中删除操作属性。

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

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