我有一个id theForm的表单,里面有以下div和提交按钮:

<div id="placeOrder"
     style="text-align: right; width: 100%; background-color: white;">
    <button type="submit"
            class='input_submit'
            style="margin-right: 15px;"
            onClick="placeOrder()">Place Order
    </button>
</div>

单击时,将调用placeOrder()函数。该函数将上述div的innerHTML更改为“processing…”(所以提交按钮现在没有了)。

上面的代码工作,但现在的问题是,我不能让表单提交!我试着把这个放在placeOrder()函数中:

document.theForm.submit();

但这行不通。

我怎样才能提交表单?


当前回答

你可以用…

document.getElementById('theForm').submit();

...但是不要替换innerHTML。你可以隐藏表单,然后插入一个处理…Span会出现在原来的地方。

var form = document.getElementById('theForm');

form.style.display = 'none';

var processing = document.createElement('span');

processing.appendChild(document.createTextNode('processing ...'));

form.parentNode.insertBefore(processing, form);

其他回答

你可以用…

document.getElementById('theForm').submit();

...但是不要替换innerHTML。你可以隐藏表单,然后插入一个处理…Span会出现在原来的地方。

var form = document.getElementById('theForm');

form.style.display = 'none';

var processing = document.createElement('span');

processing.appendChild(document.createTextNode('processing ...'));

form.parentNode.insertBefore(processing, form);

将表单的name属性设置为“theForm”,代码就可以工作了。

它在我的情况下非常有效。

document.getElementById("form1").submit();

同样,你可以在函数中使用它,如下所示:

function formSubmit()
{
    document.getElementById("form1").submit();
}
document.forms["name of your form"].submit();

or

document.getElementById("form id").submit();

你可以尝试其中任何一种……这肯定有用……

如果你的表单没有任何id,但是它有一个类似theForm的类名,你可以使用下面的语句来提交它:

document.getElementsByClassName("theForm")[0].submit();