使用提示输入值并根据 JavaScript 中的条件进行评估?
javascriptweb developmentfront end technologyobject oriented programming更新于 2026/1/6 8:12:17
假设我们使用提示获取两个值−
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了。

点击OK按钮后,就会得到结果,输出如下−

相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

