Firefox won't submit a form created by JavaScript

前端 未结 2 1219
庸人自扰
庸人自扰 2021-01-11 10:19

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 do

相关标签:
2条回答
  • 2021-01-11 10:54

    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.

    0 讨论(0)
  • 2021-01-11 11:02

    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();

    0 讨论(0)
提交回复
热议问题