【问题标题】:$( "#target" ).submit(); not working$( "#target" ).submit();不工作
【发布时间】:2016-12-05 12:25:09
【问题描述】:

由于某种原因,我不明白为什么我的表单没有使用 $( "#target" ).submit(); 提交。

<html>
<head>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js'></script>    
<script>
function saveItem()
{
    console.log($("#frm"));
    $("#frm").submit();
}
</script>
</head>
<body>
<form id="frm" action="form.php" method="post">
    <a href="javascript:;" title="Save" onclick="saveItem()">save</a>
    <button type="submit" id="submit">Save</button>        
</form>
</body>
</html>

【问题讨论】:

  • 浏览器控制台有错误吗?
  • @Mahi 为什么这可能是重复的?
  • @Archer 在其中一个答案中写道,链接本身不做任何事情,
  • id="submit" 覆盖 submit() 的 DOM 方法 form。使用不同的id

标签: jquery html forms


【解决方案1】:

表单元素中的id属性不能是submit试试别的名字。

why we shouldn't set id as submit ?

因为如果表单控件(例如提交按钮)具有提交的名称或 id,它将屏蔽表单的提交方法。

【讨论】:

  • 它可以但不应该用于嵌套在form 中的任何元素。顺便说一句,name 属性也是如此。编辑:我没有反对它......
【解决方案2】:

<form id="frm" action="form.php" method="post">
    <a href="#" title="Save" onclick="saveItem()">save</a>
</form>

如果您想通过 JS 提交表单,请删除提交按钮。并改变元素类型,

&lt;span&gt;

例如。

【讨论】:

    猜你喜欢
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    相关资源
    最近更新 更多