JQuery .trigger('submit') breaking

前端 未结 5 1259
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-21 09:48

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 clic

相关标签:
5条回答
  • 2020-12-21 10:26

    You should build the link like this:

    var link = $("<a />").addClass('swapLink')
               .atrr('href', '#')
               .text( $this.val() )
               .onclick( function() {
                 $(f).trigger('submit');
                 return false;
                });
    

    Also make sure you insert the link into the DOM and that f really is a refrence to the form.

    You could also try:

    $(f).submit();
    

    It would help if you posted the HTML used for your form.

    0 讨论(0)
  • 2020-12-21 10:28

    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.

    See this note from the 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.

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

    0 讨论(0)
  • 2020-12-21 10:30

    You'll have to use a different name for your button. "submit" is a reserved word, and using an element with the name "submit" will actually prevent jQuery from being able to submit your form at all.

    You can find a workaround at http://manicode.blogspot.com/2009/04/form-input-names-with-reserved-words.html

    0 讨论(0)
  • 2020-12-21 10:37

    $('#yourelementhere').trigger('click');

    this worked for me... not submit but click.

    0 讨论(0)
  • 2020-12-21 10:40

    if you can't change your html (for example because zend form are such smartasses...)

    it's as easy as this(change the name with the javascript before trying to submit):

    $('#submit').attr('name', 'stupid_javascript');
    
    0 讨论(0)
提交回复
热议问题