【问题标题】:Problem submitting form not working for me [closed]提交表单不适合我的问题[关闭]
【发布时间】:2020-05-31 01:43:02
【问题描述】:

我需要帮助,我正在尝试使用 jquery 提交表单,但它不适合我

<form id="form" method="post" value="" action="https://google.com">
    <input type="text" id="input">
    <input type="button" id="button" value="Submit">
</form>

<script type="text/javascript">
$(document).ready(function () {
    $( "#button" ).click(function() {
        $( "#form" ).submit();
    });
});
</script>

【问题讨论】:

  • “不工作” 不是一个非常有价值的技术问题描述,它给我们提供了很多工作价值。发生了什么或没有发生什么,您期望与此有什么不同?
  • 在什么情况下它不起作用?根本没有提交吗?它没有正确重定向吗?您真的需要使用 javascript 或 jQuery 来提交此表单吗?您是否尝试将type="submit" 添加到提交按钮?如果您尝试制作 Google 搜索表单,您可能应该阻止表单提交并将用户重定向到 https://google.com/q=&lt;search term&gt;
  • 我敢打赌,您唯一的问题是没有在 &lt;input&gt; 上使用 name。没有name,表单控件不会提交
  • @FredBrown,@charlietfl 是对的,除了script url 我不明白为什么代码不起作用!如果你愿意,你可以提出这个问题,只需使用cdn 就可以了。

标签: javascript html jquery css


【解决方案1】:

尝试使用onclick 事件。

function ClickMethod() {
  document.getElementById("form").submit();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="form" method="post" value="" action="https://google.com">
  <input type="text" id="input">
  <input type="button" id="btnSubmit" onclick="ClickMethod()" value="Submit">
</form>

【讨论】:

  • 这如何解释为什么显示的代码不起作用?
  • @charlietfl 哪个代码?我的代码或 OP 代码?
  • 当然是 OP 的代码,因为他们说它不起作用
  • 除了输入没有名称之外,OP 显示的内容应该可以工作......所以问题就变成了......究竟是什么不工作?当然你可以用另一种方式做同样的过程,但这并不能解释为什么原来的不能工作或解决 OP 的问题
  • 你怎么知道的?没有理由不应该
【解决方案2】:
<form id="form" class="anyname" method="post" value="" action="https://google.com">
    <input type="text" id="input">
    <input type="button" id="button" value="Submit">
</form>

在给表单一个类之后,你可以运行下面的 jquery

$('.anyname')[0].submit();

【讨论】:

  • jQuery 在封装原生提交的 API 中有一个 submit() 方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-09
  • 1970-01-01
  • 2013-03-07
相关资源
最近更新 更多