使用提示輸入值,並根據 JavaScript 中的條件進行評估?
假設我們使用提示獲取兩個值 −
var firstvalue = parseInt(prompt("Enter the value1"));
var secondvalue = parseInt(prompt("Enter the value2"));示例
以下為程式碼 −
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<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>
<body>
</body>
<script>
var firstvalue = parseInt(prompt("Enter the value1"));
var secondvalue = parseInt(prompt("Enter the value2"));
if (firstvalue * secondvalue > 50)
document.write("<h1>Result is correct</h1>");
else
document.write("<h1>Result is not correct</h1>");
</script>
</html>若要執行上述程式,請將檔名稱儲存為 anyName.html(index.html)。右鍵單擊該檔案,並選擇 VS Code 編輯器中的“Live Server 中開啟”選項。
輸出如下 −

輸入第一個值。我將輸入 10 −

單擊“確定”按鈕後,你需要輸入第二個值 −

輸入第二個值。我將再次輸入 10。

單擊“確定”按鈕後,你將獲得結果。輸出如下 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP