在 jquery 中提交表单 - 不适用于 firefox

发布于 2024-12-24 19:04:59 字数 687 浏览 3 评论 0原文

尝试在 JavaScript 调用中创建并提交表单。此代码在 Chrome 上运行良好,但在 Firefox 和 IE 上不起作用。我首先从 Ajax 调用 (JSON) 获取值,然后将其作为表单提交

$('<form name="acsform" action="' + JSONobject.formacsurl +'" method="POST">' + 
  '<input type="hidden" name="PaReq" value="' +JSONobject.formpareq + '">' +
  '<input type="hidden" name="TermUrl" value="'+ JSONobject.formtermUrl +'">' + 
  '<input type="hidden" name="MD" value="'+ JSONobject.formmd +'">' +
  '</form>').submit();

有什么想法吗?

只是添加...当我在该提交下方的一行中提醒某些文本时,我会在屏幕上看到警告框。浏览器似乎可以很好地浏览该代码(甚至可能提交表单),但我没有被重定向到表单 action 地址。我也没有收到任何 JS 错误。

再一次,它仅在 Firefox 和 IE 上失败,Chrome 可以正常提交和重定向。

Trying to create and submit form within a javascript call. This code works fine on Chrome but doesn't on Firefox and IE. I first get values from Ajax call (JSON) and then submit it as a form

$('<form name="acsform" action="' + JSONobject.formacsurl +'" method="POST">' + 
  '<input type="hidden" name="PaReq" value="' +JSONobject.formpareq + '">' +
  '<input type="hidden" name="TermUrl" value="'+ JSONobject.formtermUrl +'">' + 
  '<input type="hidden" name="MD" value="'+ JSONobject.formmd +'">' +
  '</form>').submit();

Any ideas?

Just to add... when I alert some text a line below that submission, I get the alert box on the screen. It may seem like browser goes through that code fine (perhaps even submits the form) but I'm not being redirected to forms action address. I don't get any JS errors either.

Once again, it fails only on Firefox and IE, Chrome does submission plus redirection fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高冷爸爸 2024-12-31 19:04:59

试试这个代码:

$(document).ready(function()
{
    /*
    *    IF IS APPEND, USE A ID OR CLASS
    */
    $('#X').append('<form id="myNewForm" class="myNewForm" method="post" action="">...</form>');
    $('#myNewForm').submit();

    //OR USE CLASS NAME
    $('form.myNewForm').submit();

    /*
    *    IF NOT APPEND, USE A ID OR CLASS
    */
    //USEA ID
    $('#myForm').submit();

    //USE A CLASS NAME
    $('form.my-form').submit();

    //USE A ELEMENT( ALL )
    $('form').submit();

});

再见!

Try this code:

$(document).ready(function()
{
    /*
    *    IF IS APPEND, USE A ID OR CLASS
    */
    $('#X').append('<form id="myNewForm" class="myNewForm" method="post" action="">...</form>');
    $('#myNewForm').submit();

    //OR USE CLASS NAME
    $('form.myNewForm').submit();

    /*
    *    IF NOT APPEND, USE A ID OR CLASS
    */
    //USEA ID
    $('#myForm').submit();

    //USE A CLASS NAME
    $('form.my-form').submit();

    //USE A ELEMENT( ALL )
    $('form').submit();

});

Bye!

花间憩 2024-12-31 19:04:59

坦率地说,我不确定 stackoverflow 上的问题是否可以通过评论来解决,以及是否可以通过回答自己的问题来解决,但我真的不想创建一个新问题并复制这个问题的内容,

我有这段代码,基于 Olaf 的解决方案,

    $('#basic-modal-content').append('<form id="acsform" name="acsform" action="' + JSONobject.formacsurl +'" method="POST">' + 
    '<input type="hidden" name="PaReq" value="' +JSONobject.formpareq + '">' +
    '<input type="hidden" name="TermUrl" value="'+ JSONobject.formtermUrl +'">' + 
    '<input type="hidden" name="MD" value="'+ JSONobject.formmd +'">' +
    '</form>');
    $('#acsform').submit();

这在 IE、Firefox、Opera 和 Chrome 上运行良好。但今天,我们收到一位客户的报告,称该过程无法在他的 iPad 上运行。不幸的是,我无法获取他在该 ipad 上使用的到底是什么浏览器的信息(而且我自己没有 ipad 可以检查),但似乎这段代码不起作用。日志中的最后一个操作是创建 JSON 对象的 ajax 操作,上面的 js 在收到该 JSON 后被触发。那是我的猜测。
您是否清楚地知道此表单提交在 ipad 默认浏览器(无论该浏览器是什么)上存在问题?

Frankly, I'm not sure if question on stackoverflow can be bumped by commenting it and if it's allowed to bump by answering your own question, but don't really want to create a new one and copy content of this one

I have this code, based on a solution from Olaf

    $('#basic-modal-content').append('<form id="acsform" name="acsform" action="' + JSONobject.formacsurl +'" method="POST">' + 
    '<input type="hidden" name="PaReq" value="' +JSONobject.formpareq + '">' +
    '<input type="hidden" name="TermUrl" value="'+ JSONobject.formtermUrl +'">' + 
    '<input type="hidden" name="MD" value="'+ JSONobject.formmd +'">' +
    '</form>');
    $('#acsform').submit();

This worked all fine on IEs, Firefox, Opera and Chrome. But today, we got a report from a customer, that the process didn't work from his ipad. I unfortunately cannot get information what browser exactly is he using on that ipad (and I don't have an ipad myself to check) but it seems that this piece of code didn't work. Last operation in the logs is ajax operation that creates JSON object and the js above is fired after receiving that JSON. It's my guess then.
Do you know anything from top of your head in regard to this form submission being a problem on ipad default browser (whatever that browser is)

夜唯美灬不弃 2024-12-31 19:04:59

使用带 ID 的原生 Submit()。

$('<form/>', {name:"acsform", id:"acsform", method:"POST", action: JSONobject.formacsurl})
.append(
  $('<input/>', {type:"hidden", name:"PaReq", value: JSONobject.formpareq}),
  $('<input/>', {type:"hidden", name:"TermUrl", value: JSONobject.formtermUrl}),
  $('<input/>', {type:"hidden", name:"MD", value: JSONobject.formmd})
).appendTo(document.body)
$("form#acsform")[0].submit()

Use native submit() with ID.

$('<form/>', {name:"acsform", id:"acsform", method:"POST", action: JSONobject.formacsurl})
.append(
  $('<input/>', {type:"hidden", name:"PaReq", value: JSONobject.formpareq}),
  $('<input/>', {type:"hidden", name:"TermUrl", value: JSONobject.formtermUrl}),
  $('<input/>', {type:"hidden", name:"MD", value: JSONobject.formmd})
).appendTo(document.body)
$("form#acsform")[0].submit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文