jQuery submit form by clicking link issue

大憨熊 提交于 2020-01-25 10:06:11

问题


I'm trying to submit a form by clicking on a link. I disabled the redirection, but for some reason .submit() is not working... Here is what I have tried:

Effect: redirection stops, no form submission, no error message, stuck on the form page:

$('.jsubmit').click( function(e) {
    e.preventDefault();
    $('form#fadmin').submit();
});

Effect: URL redirection, form not submitted, no error message

$('.jsubmit').click( function(e) {
    $('form#fadmin').submit();
});

Effect: redirection stops, no form submission, no error message, stuck on form page:

$('.jsubmit').click( function(e) {
    $('form#fadmin').submit();
    return false;
});

The form:

<form action="" method="post" name="fadmin" class="inputform" id="fadmin">...</form>

And a bunch of other combination including trigger(), reversing the preventDefault() with unbind(). The only way I was able to submit the form was to trigger a click on the submit button but that is not really a solution in my case, because I need to use this on multiple pages and adding the button then hiding it is not something I would like to do on every page...

I have tried to run them in Firefox and IE with the same result.

Some other JS, jQ codes being used are: default bootstrap and respond provided by ZendFramework2 and ZFTables. Any help would be much appreciated!

EDIT:

The form had the following submit button:

<input id="mysubmit" type="submit" value="Register" name="submit" />

After removing this my first example above worked perfectly. Strange because there was no other forms or submit buttons on the page and nothing with the same name, id, type...


回答1:


The issue is probably that you are trying to call submit() on a jQuery object, not the form DOM element.

Try this as your second line of code:

$('form#fadmin')[0].submit();


来源:https://stackoverflow.com/questions/25804778/jquery-submit-form-by-clicking-link-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!