Javascript JS - 如何提交表单 onclick 并发送提交按钮

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

JS - How to submit form onclick and send submit button

javascripthtmlformsbuttonsubmit

提问by MakoBuk

I need to submit form by button which is out of scope of the form by JavaSript.

我需要通过按钮提交表单,这超出了 JavaSript 的表单范围。

<form>
  <input name="data" type="text">
  <input type="submit" name="send" id="send-button" style="display: none">
</form>

<button onclick="$('#send-button').click() || .submit()"></button>

The button #send-buttonis visible and I need to send whole form to server. It means I receive $dataand $send(both).

按钮#send-button是可见的,我需要将整个表单发送到服务器。这意味着我收到$data$send(两者)。

Now I am getting just the $data.

现在我只得到$data

Any idea?

任何的想法?

回答by Man Programmer

You can do with javascript form method

您可以使用 javascript 表单方法

<button onclick="document.forms[0].submit()"></button>

if you have multiple forms in the document then set id to the form

如果文档中有多个表单,则将 id 设置为表单

<form id="form1">
  <input name="data" type="text">
  <input type="submit" name="send" id="send-button" style="display: none">
</form>
<button onclick="document.getElementById("form1").submit()"></button>

回答by KurinchiMalar

Give the form a name:

为表单命名:

    <form name="myform">
        <a href="javascript: submitform()">Submit</a>
    </form>

Submit form function:

提交表单功能:

    <script type="text/javascript">
        function submitform()
        {
           document.myform.submit();
        }
    </script>

Invoke submission:

调用提交:

    document.forms["myform"].submit();

回答by manish

you should add value attribute on submit button like that

你应该像这样在提交按钮上添加 value 属性

<input type="submit" name="send" value="xyz" id="send-button" style="display: none">

i think you will received both values

我想你会收到两个值