jQuery .trigger('submit')打破 [英] JQuery .trigger('submit') breaking

查看:131
本文介绍了jQuery .trigger('submit')打破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,因为我正在处理的所有表单都有一个包含"name ="submit""属性的提交按钮,所以当我单击应该触发表单提交的链接时,触发器提交就中断了

Im finding that because all the forms that Im working on have a submit button which inlcudes a 'name="submit"' attribute, that the trigger submit is breaking when I click on the link that is supposed to trigger the form submit.

有人知道我该如何解决这个问题.

Does anyone know how I can get round this.

以下是我使用的JQuery代码:

The JQuery code below is what Im using:

$('#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'});
            }
        )

对此表示感谢.谢谢

嗨.请查看下面的链接:

Hi. check out the link below:

http://www.mr-scraggy.com/test.html

在此页面上,您可以看到正在使用的表单.在提交按钮中没有name ="submit"的最上面的表单起作用.包含名称="submit"的底部形式不包含. (当我说有效时,该表单并未连接到任何脚本,但是您可以看到顶部表单中的Submit链接确实具有某些功能".)

on this page you can see the form in action. The top form which does not have a name="submit" in the submit button works. The bottom form which includes the name="submit" does not. (when I say work, the form is not hooked up to any scripts, but you can see that the submit link in the top form does 'something').

另请参见下面的html表格:

see also below the form 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>

推荐答案

这是因为您为提交"按钮指定了提交"的名称.因为这已经是表单对象的属性,所以它会与Submit事件的绑定混乱.

It's because you've given your submit button a name of "submit". Because this is already a property of the form object, it messes with the binding of the submit event.

请参见 jQuery文档

表单及其子元素应 不使用输入名称或ID 与表单的属性冲突, 例如提交,长度或方法. 名称冲突可能会引起混淆 失败.

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"),它将起作用.

Change the name of your submit button to anything else ("form_submit", for example) and it'll work.

这篇关于jQuery .trigger('submit')打破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆