Firefox 不会提交由 JavaScript 创建的表单
当事件发生时,我需要创建一个包含一些输入的表单。我的代码如下。
Chrome 提交正常 - 警报框显示并且页面发生变化。
Firefox 不起作用 - 会显示警告框,但页面保持不变。 如何让 Firefox 提交表单?
var idsInput = document.createElement('input');
idsInput.name = 'itemIds';
idsInput.value = ids;
var quantityInput = document.createElement('input');;
quantityInput.name = 'quantity';
quantityInput.value = 1;
var authTokenInput = document.createElement('input');
authTokenInput.name = 'authenticityToken';
authTokenInput.value = '${session.getAuthenticityToken()}';
var submitInput = document.createElement('input');
submitInput.type = 'submit';
submitInput.value = 'anything';
var form = document.createElement('form');;
form.action = '@{Checkout.setItemsQuantityHandler}';
form.method = 'POST';
form.elements[0] = idsInput;
form.elements[1] = quantityInput;
form.elements[2] = authTokenInput;
form.elements[3] = submitInput;
form.submit();
alert('after submit()'); // for debugging only
I need to create a form with a few inputs when an event happens. My code is below.
Chrome submits fine - the alert box shows and the page changes.
Firefox does not work - the alert box shows but the page stays the same. How can I get Firefox to submit the form?
var idsInput = document.createElement('input');
idsInput.name = 'itemIds';
idsInput.value = ids;
var quantityInput = document.createElement('input');;
quantityInput.name = 'quantity';
quantityInput.value = 1;
var authTokenInput = document.createElement('input');
authTokenInput.name = 'authenticityToken';
authTokenInput.value = '${session.getAuthenticityToken()}';
var submitInput = document.createElement('input');
submitInput.type = 'submit';
submitInput.value = 'anything';
var form = document.createElement('form');;
form.action = '@{Checkout.setItemsQuantityHandler}';
form.method = 'POST';
form.elements[0] = idsInput;
form.elements[1] = quantityInput;
form.elements[2] = authTokenInput;
form.elements[3] = submitInput;
form.submit();
alert('after submit()'); // for debugging only
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
FF 要求它已经在 DOM 中。将 form 设置为
display:none
并将其添加到 DOM 中的现有元素中,然后提交。FF requires it to be in the DOM already. Set form to
display:none
and add it to an existing element in DOM and then submit it.试试这个...
var idsInput = document.createElement('input');
idsInput.name = 'itemIds';
idsInput.value = ids;
var amountInput = document.createElement('input');
amountInput.name = '数量';
数量输入.值 = 1;
var authTokenInput = document.createElement('input');
authTokenInput.name = 'authenticityToken';
authTokenInput.value = '${session.getAuthenticityToken()}';
var SubmitInput = document.createElement('input');
commitInput.type = '提交';
commitInput.value = 'anything';
var form = document.createElement('form');
form.action = '@{Checkout.setItemsQuantityHandler}';
form.method = 'POST';
form.elements[0] = idsInput;
form.elements[1] = amountInput;
form.elements[2] = authTokenInput;
form.elements[3] = 提交输入;
document.body.appendChild(form);
表单.提交();
Try This...
var idsInput = document.createElement('input');
idsInput.name = 'itemIds';
idsInput.value = ids;
var quantityInput = document.createElement('input');
quantityInput.name = 'quantity';
quantityInput.value = 1;
var authTokenInput = document.createElement('input');
authTokenInput.name = 'authenticityToken';
authTokenInput.value = '${session.getAuthenticityToken()}';
var submitInput = document.createElement('input');
submitInput.type = 'submit';
submitInput.value = 'anything';
var form = document.createElement('form');
form.action = '@{Checkout.setItemsQuantityHandler}';
form.method = 'POST';
form.elements[0] = idsInput;
form.elements[1] = quantityInput;
form.elements[2] = authTokenInput;
form.elements[3] = submitInput;
document.body.appendChild(form);
form.submit();