簡體   English   中英

為什么我在 html 中看不到表單結果?

[英]why can't I see the form results in html?

我是 html 的初學者,我試圖了解 html 中的 forms 的工作原理,但如下面的代碼所示,我在單擊提交按鈕后無法獲得表單結果。 你能告訴我為什么它不起作用以及如何解決它嗎?

索引.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>html/css</title>
</head>
<body>

   <form action="result.html" method="get">
    <label for="N">Name:</label>
       <input type="text" id="N" name="Name" required>
      <br>

       <label  for="P">Phone number:</label>

       <input type="number" id="P" name="phone_nos" required> 
       <br>

    <button>submit</button>
       <br>
   </form>
</body>
</html>

結果.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="results"></div>
   <a href="/">Go Back</a>
    <script>
        const resultsList = document.getElementById('results')
        new URLSearchParams(window.location.search).forEach(
(value,name)=>{ resultsList.append('${name} : ${value}')
        resultsList.append(document.createElement('br'))
        })
    </script>
</body>
</html>

這是點擊提交按鈕后屏幕上顯示的內容

${name}:${value} ${name}:${value}

嘗試將表單方法更改為 POST 而不是 get

--> <form action="result.html" method="post">

submit 類型的<input>元素呈現為按鈕。 當單擊事件發生時(通常是因為用戶單擊了按鈕),用戶代理會嘗試將表單提交給服務器。

改變

<button>submit</button>

<input type="submit">

暫無
暫無

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

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