代码将无法运行,因为我无法在document.write中输出多个变量

时间:2013-03-13 02:31:37

标签: javascript

当我尝试在代码的document.write部分中显示多个变量时,我的代码将无法运行。我很确定我做的一切都很正确。


<script type="text/javascript">
var name = prompt("Welcome to the Fruity Store. What is your name?","");
var product = prompt("What is the name of the product you would like?","");
var price = 1*prompt("How much does it cost?","");
var quantity = 1*prompt("How many of the fruit would you like?","");
var discount = 1*prompt("What was the discount of the product in decimal form?","");
var costoforder = (price*quantity);
var discounted = (price*quantity*discount);
var totalorder = (costoforder - discounted);

document.write("Thank you for placing an order with us " +name ) 
document.write("<p>The cost of buying " +quantity "of " +product "is " +costoforder    </p>)
document.write("<p>The discount for this purchase is " +discounted </p>)
document.write("<p>With discount, your total order cost is " +totalorder</p>)

</script>

3 个答案:

答案 0 :(得分:1)

你在字符串连接中缺少一些加号。

"<p>The cost of buying " + quantity + " of " + product + " is " + etc.

答案 1 :(得分:0)

您的变量后缺少“+”符号。

你把

"String" + variable "string"

而不是

"string" + variable + "string"

在document.write语句中多次

答案 2 :(得分:0)

您需要使用加号+进行字符串连接。你也错过了;在陈述之后。

<script type="text/javascript">
var name = prompt("Welcome to the Fruity Store. What is your name?","");
var product = prompt("What is the name of the product you would like?","");
var price = 1*prompt("How much does it cost?","");
var quantity = 1*prompt("How many of the fruit would you like?","");
var discount = 1*prompt("What was the discount of the product in decimal form?","");
var costoforder = (price*quantity);
var discounted = (price*quantity*discount);
var totalorder = (costoforder - discounted);

document.write("Thank you for placing an order with us " + name ); 
document.write("<p>The cost of buying " + quantity + "of " + product + "is " + costoforder   + "</p>");
document.write("<p>The discount for this purchase is " + discounted + "</p>");
document.write("<p>With discount, your total order cost is " +totalorder + "</p>");

</script>