獲取 HTML 表單的值並在 JavaScript 中在控制檯上顯示?


如需獲取 HTML 表單的值,請使用 value 屬性。假定我們的輸入型別如下 −

<input type="text" id="getValues" value="My Name is John Smith" />

我們需要將上述值“My name is John Smith”顯示在控制檯上。

示例

 演示

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fontawesome/
4.7.0/css/font-awesome.min.css">
</head>
<body>
<input type="text" id="getValues" value="My Name is John Smith" />
<script>
   var originalvalue = document.getElementById("getValues").value;
   console.log("The value in the text box is ="+originalvalue);
</script>
</body>
</html>

如需執行上述程式,請儲存該檔案為“anyName.html(index.html)”並右鍵單擊該檔案。在 VS Code 編輯器中選擇“使用 Live Server 開啟”選項。

輸出

這將產生以下輸出 −


更新於: 2020-09-11

5000+ 檢視量

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.