使用提示輸入值,並根據 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。

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

更新日期:2020-10-03

191 次瀏覽

開啟你的職業生涯 生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.