Cannot programmatically submit form [duplicate]

半腔热情 提交于 2019-12-12 23:31:52

问题


Cannot get my form to submit programmatically, despite trying several ways. Tried pure JS, also tried jQuery. No success.

I have this form tag :

<form action="https://docs.google.com/forms/d/13zex8ZsEsnZz3A8uB4jU4oDb5wZfaqqq2Pq2CGlIe6M/formResponse" method="POST" id="ss-form" target="_self" onsubmit="" name="eForm">
    <!--My Form Stuff-->
    <input type="submit" name="submit" value="Submit" id="ss-submit">
</form>

Here is what i've tried :

/*jQuery*/
$('#ss-form').submit();

/*Javascript*/
document.getElementById('ss-form').submit();
document.eForm.submit();

None of them work. Not sure why, but I am assuming it has something to do with me trying to submit a google form. If I physically click the submit everything works fine.

Any and all help is greatly apprecaiated.


回答1:


The problem is that you have a field (HTMLInputElement) with name submit in the form. That's why document.getElementById('ss-form').submit is not a function but an object.

So, you get the following error:

TypeError: object is not a function

The solution is to remove that element. We should be careful to verify if browser thinks it's an element and not a function:

if (typeof document.getElementById('ss-form').submit === "object") {
    document.getElementById('ss-form').submit.remove();
}
document.getElementById('ss-form').submit();


来源:https://stackoverflow.com/questions/24370355/cannot-programmatically-submit-form

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