代码之家  ›  专栏  ›  技术社区  ›  Pharylon

程序创建的表单不能在Firefox或Internet Explorer中提交

  •  0
  • Pharylon  · 技术社区  · 10 年前

    我想使用javascript创建一个表单,然后提交该表单。在提交的另一边是一个API,它以编程方式为用户构建PDF。

    以下功能适用于Chrome,但不适用于IE或Firefox:

      function downloadPdf () {
    
        var form = document.createElement("form");
        form.setAttribute("action", "http://internal.site.com/downloadPdf");
        form.setAttribute("method", "POST");
        form.setAttribute("target", "_blank");
    
        form.submit();
      };
    

    我在这里做错了什么?

    记录在案,这是一个简化版的函数,没有显示所有复杂的内容 为什么? 我想这样做

    1 回复  |  直到 10 年前
        1
  •  1
  •   Marcos Pérez Gude    10 年前

    您可以将表单附加到隐藏元素中,然后提交。如果DOM中不存在任何内容,则无法提交该内容。Chrome有缺陷,即使你认为这是正确的行为,但事实并非如此。

    var form = document.createElement("form");
    form.setAttribute("action", "http://internal.site.com/downloadPdf");
    form.setAttribute("method", "POST");
    form.setAttribute("target", "_blank");
    form.appendChild(document.getElementsByTagName('body')[0]); // append it
    form.style.display = "none"; // hide it
    
    form.submit();
    
    推荐文章