jQuery,使用按钮提交表单

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

jQuery, submit form with a button

jquery

提问by Someone

I have a form which contains the method "POST" and action ="abc.php" and button type of <input type ="button">I have a handler when i cick that button i want to send a request to abc.php but nothing is happening no action is being prformed.I dont want to change the <input type ="button">to <input type="submit>.How do i submit the Form .Here is the code

我有一个表单,其中包含方法“POST”和 action ="abc.php" 和按钮类型,<input type ="button">当我点击该按钮时,我有一个处理程序我想向 abc.php 发送请求,但没有发生任何操作prformed.I don't want to change the <input type ="button">to <input type="submit>.How do I submit the Form .Here is the code

<form name= "form1" id ="form1" action ="abc.php" method="post">
<input type ="button" id="mybutton" value ="Add"> 
......
//All form Elements.
</form>

$(document).ready(function() {
    //Load all elements
});
$("#mybutton").click(function(e){
    alert(true);
    //$("#frmpohdr").submit();
});

The Above Statement is giving error and i know we need to have button type of submit for this method.How do i submit the Form to the abc.php when i click the button .I have tried all $.ajax methods

上面的语句给出了错误,我知道我们需要为此方法提交按钮类型。当我单击按钮时,我如何将表单提交到 abc.php。我已经尝试了所有 $.ajax 方法

回答by ozke

Have you tried putting all the code inside ready?

你有没有试过把所有的代码都准备好?

Also, if the form's id is form 1 you should do this:

此外,如果表单的 id 是表单 1,您应该这样做:

$("#form1").submit();

And to avoid the buttons default's behaviour you should also add this link inside click's function:

为了避免按钮默认行为,您还应该在 click 函数中添加此链接:

e.preventDefault();

I also recommend you having a look at jQuery Form Plugin: http://jquery.malsup.com/form/

我还建议您查看 jQuery 表单插件:http: //jquery.malsup.com/form/

I hope i helped :)

我希望我有所帮助:)

回答by kiran vennampelli

jq("#showDetail").click(function() {
            jq('#formName').submit();
});
<input type="button" id="showDetail" class="secondarybutton" value="Done" />

回答by btrandom

You will either submit the form through the HTML form (i.e. change the input type = submit) Or you could use the $.post/$.ajax

您将通过 HTML 表单提交表单(即更改输入类型 = 提交)或者您可以使用 $.post/$.ajax