Javascript 提交按钮验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8638040/
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-08-24 06:50:16  来源:igfitidea点击:

validation on submit button

javascripthtmlforms

提问by Rocky

I have a submit button like this:

我有一个像这样的提交按钮:

<input class="create_button" name="commit" 
       onclick="return validate_activity();" 
       type="submit" value="Save">

I found that this button will always send request to the server whatever the validate_activity() return true or false?

我发现无论validate_activity() 返回true 还是false,这个按钮总是会向服务器发送请求?

What's the problem here?

这里有什么问题?

UPDATE

更新

Actually, I made a mistake in my validate_activity(), it makes me think it returned false, but it not.

实际上,我在我的 validate_activity() 中犯了一个错误,这让我认为它返回了 false,但事实并非如此。

回答by Mr. BeatMasta

try doing same without return, e.g. onclick="validate_activity();"and check if your function returns false in case of invalidity

尝试在不返回的情况下执行相同的操作,例如onclick="validate_activity();"并检查您的函数是否在无效的情况下返回 false

回答by King

<input type="button" name="commit"
value="Save"
onclick="validate_activity(myform)" />

Inside the Javascript, when validated, do form.submit()or else return false.

在 Javascript 中,验证后,执行form.submit()否则返回 false。

I hope this helps ! This will validate your form and submit it. This submit will fail if the Javascript is disabled in your browser.

我希望这有帮助 !这将验证您的表单并提交。如果您的浏览器禁用了 Javascript,则此提交将失败。

if you want the submit to proceed even if the javascript is disabled in the client browser ( means no validation but will submit), then

如果您希望即使在客户端浏览器中禁用 javascript 也继续提交(意味着没有验证但会提交),那么

<input type="button" name="commit"
value="Save"
onclick="validate_activity(myform);return false;" />

I hope no validation shouldn't hit you at all. But then javascript is just client side and I hope you will have serious sanity checks on the server side anyways and of many, I think people use it for very basic field checks.

我希望没有验证不应该打你。但是 javascript 只是客户端,我希望您无论如何都会在服务器端进行认真的健全性检查,我认为人们将它用于非常基本的字段检查。

回答by Ramesh Kotha

Try this...

尝试这个...

<form action="...." onsubmit="submitForm()" name="form">

<input type="submit" value="Save">

</form> 


function submitForm(){

    if(validationfails){

        return false;
    }
    else  {
        document.form.submit();
        return true;
    }
 }

回答by Lion

If you're doing form validation then validating it on button's onclickevent, the way you're are trying makes no sense better you should use it on <form></form>onsumbmitevent like this.

如果您正在进行表单验证,然后在按钮的onclick事件上对其进行验证,那么您尝试的方式毫无意义,您应该在这样的<form></form>onsumbmit事件上使用它。

<form action="someURL" method="post" onsubmit="return validate_activity();">

</form>

回答by zuo

I guess the problem is onclick="return validate_activity();", this return statement gets executed when clicking the submit button (not return false). The correct way is onclick="validate_activity();"and return false in validata_activityfunction, which will stop the default submit action.

我想问题是onclick="return validate_activity();",当单击提交按钮(不返回 false)时,会执行此返回语句。正确的方法是 onclick="validate_activity();"validata_activity函数中返回false ,这将停止默认提交动作。

回答by Gabriele Petrioli

You should use the submitevent of the form for validation..

您应该使用submit表单的事件进行验证..

<form action="..." method="..." onsubmit="return validate_activity()">
    <...>
    <input class="create_button" name="commit" type="submit" value="Save" />
    <...>
</form>