简体   繁体   English

两个功能的Javascript按钮。 提交和点击

[英]Javascript button for two functions. Submit & OnClick

We are trying to link our form's submit button to work as a submit button and function the following Javascript.我们正在尝试将表单的提交按钮链接为提交按钮并使用以下 Javascript。

Button:按钮:

<button type="submit" id="btn_submit" class="button button--red">Sign Up</button>

It works when we remove the id="btn_submit" to submit, but when we add the id="btn_submit" it implements the Javascript.当我们删除要提交的 id="btn_submit" 时它起作用,但是当我们添加 id="btn_submit" 时它实现了 Javascript。 How could we have the one button achieve both functions?如何让一个按钮实现两种功能?

Javascript (beginning): Javascript(开始):

$("#btn_submit").on('click', function() {

Many, Many thanks!非常感谢!

$("#btn_submit").on('click', function(e) { 
e.preventDefault(); //prevent the form from submitting
//do your code here
$('form#myForm').submit(); //submit your form here

One way to do this is dont submit your form on button click after doing all the other code you can submit your form manually一种方法是在完成所有其他代码后不要在按钮单击时提交表单,您可以手动提交表单

$("#btn_submit").on('click', function(e) {
    e.preventDefault();

    //do stuff

    $("#form").submit();
}

Where "#form" represents the id of your form.其中“#form”代表表单的 id。 So if your form is "my_form", then change the above to read $("#my_form").submit();因此,如果您的表单是“my_form”,则将上面的内容更改为 $("#my_form").submit();

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

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