我发现,因为我正在处理的所有表单都有一个包含“name =“submit””属性的提交按钮,所以当我单击应该触发表单提交的链接时,触发器提交会中断.
有谁知道我该如何解决这个问题。
下面的 JQuery 代码是我使用的:
$('#login_form :submit').each(
function()
{
var $this = $(this);
var f = this.form;
var link = $('<a class="swap_link" href="#">' + $this.val() + '</a>')
link.click(function()
{
$(f).trigger('submit');
return false;
}
)
$this.after(link).css({position:'absolute', top:'-2000px'});
}
)
感谢对此的任何帮助。谢谢
嗨。查看下面的链接:
http://www.mr-scraggy.com/test.html
在此页面上,您可以看到正在运行的表单。提交按钮中没有 name="submit"的顶部表单可以正常工作。包含 name="submit"的底部表单没有。 (当我说“工作”时,该表单未连接到任何脚本,但您可以看到顶部表单中的提交链接执行“某些操作”)。
另请参阅下面的 html 表单:
<form id="swaplink" name="login" action="/action/login.php" method="post" accept-charset="utf-8" class="clearfix">
<input type="hidden" name="redirecturl" id="redirecturl" value="{$redirecturl}" />
<h2>My Account Login</h2>
<ul id="login_form">
<li>
<label for="username">Username:</label>
<input type="text" name="username" id="username" class="input-text" maxlength="32" />
</li>
<li>
<label for="password">Password:</label>
<input type="password" name="password" id="password" class="input-text" maxlength="32" />
</li>
<li>
<input type="submit" name="submit" value="Login" />
</li>
</ul>
{$failed_message}
</form>
最佳答案
这是因为您将提交按钮命名为“提交”。因为这已经是表单对象的属性,所以它与提交事件的绑定(bind)混淆。
请参阅 jQuery docs 中的此注释:
Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method. Name conflicts can cause confusing failures.
将提交按钮的名称更改为其他名称(例如“form_submit”),它就会起作用。
关于JQuery .trigger ('submit' )破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/729330/