jQuery 按钮 onclick this.form.submit 调用验证函数

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

button onclick this.form.submit call validate function

jqueryformssubmit

提问by Kashif Hashmi

I want to call validate function on form submit. When i write following code.

我想在表单提交时调用验证函数。当我编写以下代码时。

<form onSubmit = 'return validate();'>
<input type='url'>
<input type='submit' value='Submit'>
</form>

Work fine. But if i have input type button. And I write following code.

工作正常。但是如果我有输入类型按钮。我写了以下代码。

<form onSubmit = 'return validate();'>
<input type='url'>
<input type='button' onclick = this.form.submit(); value='Submit'>
</form>

Not working.

不工作。

回答by Priya Jayapal

Put this.form.submit();in quotes,

加上 this.form.submit();引号,

 <form onSubmit = 'return validate();'>
    <input type='url'>
    <input type='button' onclick = 'this.form.submit();' value='Submit'>
    </form>

its worked fine for me.

它对我来说很好用。

回答by Vigneshwaran

<form id="myForm">
    <input type="url">
    <input type="submit" id="submitBtn" value="Submit">
</form>

Jquery:

查询:

$(document).ready(function(){
 $("#submitBtn").click(function(e)({
   $("#myForm").submit();
 )};
)};

回答by Vigneshwaran

$(document).ready(function(){
 $("#submitBtn").click(function(e)({
   if(validate())
   {
     $("#myForm").submit();
   }
 )};
)};

The validate function should return true on successful execution only then the form will get submit.

验证函数应该在成功执行时返回真,然后表单才会被提交。

回答by Albuquerque Web Design

all function declarations start with a {

所有函数声明都以 { 开头

    $(document).ready(function(){
     $("#submitBtn").click(function(e){
       $("#myForm").submit();
     });
    });