我对HTML非常陌生,尤其是JavaScript,我已经遇到麻烦了。如何显示通过promt从用户那里获取的所有数据。据我所知,似乎没有什么能奏效。这是密码。
function myFunction() {
var userinput;
var userName = prompt("Please enter your name to proceed");
if (userName == true)
{
document.write ("Welcome to Office Cheapo, "+ userName + ". Here is your invoice:");
}
var notebook = prompt("How many notebooks do you want to buy?" , "1 to 10");
if (notebook == true){
document.write ("Notebooks bought -- "+ notebook);
}
var pens = prompt("How many pens do you want to buy?", "1 to 10");
if (pens == true){
document.write ("Pens bought -- "+ pens);
}
var usb = prompt("How many USB Flash Drives do you want to buy?", "1 to 10");
if (usb == true){
var taxusb = (usb*6.75)*0.05;
}
document.write ("USBs bought -- "+ usb);
}<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Cheapo Order</title>
</head>
<body>
<div>
<button onclick="myFunction()">Make an order</button>
</div>
</body>
</html>
发布于 2017-05-12 02:10:18
userName将是一个字符串或null (如果用户取消了提示符)。因此,username == true将永远是假的,您的document.write永远不会被执行。
你想要的是:
if (userName) {这将执行您的块,只要userName不是一个假值。
以下都是虚假的价值观:
因此,只要用户输入某些内容并在提示中按OK,条件就会得到满足,并且在if分支中执行您的代码。
https://stackoverflow.com/questions/43928169
复制相似问题