我是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>
&#13;
答案 0 :(得分:0)
userName
将是字符串或null
(如果用户取消了提示)。因此,username == true
始终为false,您的document.write
将永远不会被执行。
您可能想要的是:
if (userName) {
如果userName不是假值,这将执行您的块。
以下是所有假值:
因此,只要用户输入内容并在提示符中按“确定”,就会满足条件并执行if分支中的代码。