動態產生Html元素實現Post操作(createElement)

來源:互聯網
上載者:User
有時,你需要Post資料到另一個頁面上,那麼你就需要構建一個Form表單<form id="postform" name="postform" method="post">

<input name="msg" value=""/>
</form>

但如果當前頁面是用js打到頁面上的,那麼你在用js提交時不起作用document.write("<form ..."
//document.write("<iframe src=\"about:blank\" name=\"hiddenFrame\" id=\"hiddenFrame\" width=\"0\" height=\"0\" frameborder=\"0\"></iframe>");

用如下js提交不起作用,因為打到頁面上的form不是一個對象,而是一個字串//    theForm.action = "http://msg.baihe.com/tortoise/pages/tortoise/sm_gb2312.jsp?ReturnURL="+strReturnURL;
//    document.getElementById("Pathid").value="3070";
//    document.getElementById("Title").value="你好!";
//    document.getElementById("Content").value="我把你設為重點關注了,咱們聊聊吧:)";
//    document.getElementById("CloseWindow").value="1";

所以你需要自己動態建立form對象,用如下方法實現:var form_feedback = document.createElement("form");
    document.body.appendChild(form_feedback);
        
    var i = document.createElement("input");
    i.type = "hidden";
    i.name = "Title";
    i.value = "你好!";
    form_feedback.appendChild(i);
    
    
    var j=document.createElement("input");
    j.type="hidden";
    j.name="Content";
    j.value="我把你設為重點關注了,咱們聊聊吧:)";
    form_feedback.appendChild(j);
    
    var hiddenIframe=document.createElement("iframe");
    hiddenIframe.src="about:blank";
    hiddenIframe.name="hiddenFrame";
    hiddenIframe.id="hiddenFrame";
    hiddenIframe.width="0";
    hiddenIframe.height="0";
    hiddenIframe.frameborder="0";
    form_feedback.appendChild(hiddenIframe);
    
    
    form_feedback.action = "http://msg.baihe.com/tortoise/pages/tortoise/sm_gb2312.jsp?ReturnURL=";
    form_feedback.target = "hiddenFrame";
    form_feedback.method = "post";
    form_feedback.submit();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.