簡體   English   中英

如何從另一個頁面的表單中獲取數據(JavaScript)

[英]How to get data from a form in another page (JavaScript)

我的英語不夠好,所以希望您能理解我的問題...

我有一個帶有表單的頁面“ A”。 當我單擊“提交”時,我希望它打開/重定向到頁面“ B”,並且要在頁面“ B”上顯示頁面“ A”中的表單數據...我想使用JavaScript來做到這一點,不在jQuery左右。 我嘗試使用window.opener或類似的東西...接下來,顯示示例代碼。

頁面“ A”:

<html>
    <head>
    </head>
    <body>
        <form method="post" name="form1" id="form1">
            <input type="text" name="field1">
            <input type="submit" name="submit" onclick="window.open('show.html');">
        </form>
    </body>
</html> 

頁面“ B”:

<html>
    <head>
    </head>
    <body>
        <script>
            var x = opener.document.forms["form1"]["field1"].value;
    document.write(x);
        </script>
    </body>
</html>

頁面“ B”可以顯示頁面“ A”上的數據非常重要。 我不能使用PHP或其他技術...:/

我希望你能理解我的要求。 謝謝!

**更新

正如Sagar Hani指出的,解決方案(至少對我而言)是localStorage。 無論如何,感謝回答的人!

  • 永遠都無法從客戶端獲取POST數據(使用javascript或任何其他客戶端語言)
  • 您必須使用服務器端語言來獲取發布數據(例如PHP,nodeJs)。
  • 但是您只需從客戶端(javascript)獲取GET數據即可。

以下是您想要的確切解決方案。

頁面“ A”(a.html)

   <html>
        <head>
        </head>
        <body>
            <form action="b.html" method="get" name="form1" id="form1">
                <input type="text" name="field1">
                <input type="submit" name="submit">
            </form>
        </body>
    </html> 

頁面“ B”(b.html)

<script>

alert(findGetParameter("field1"))

function findGetParameter(parameterName) {
    var result = null,
        tmp = [];
    location.search
        .substr(1)
        .split("&")
        .forEach(function (item) {
          tmp = item.split("=");
          if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
        });
    return result;
}

</script>

不能是PHP嗎? 使用PHP,您還可以將信息發送到頁面B並向用戶顯示所有信息。 實際上,由於它是服務器端的過程,因此對用戶更友好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM