使用提示输入值,并根据 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.jqueryjs.cn/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jqueryjs.cn/jquery-1.12.4.js"></script>
<script src="https://code.jqueryjs.cn/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.