2017-04-24 41 views
0

我最近开始学习HTML和JavaScript和我在记事本中创建一个简单的影音出租脚本++的过程。创建脚本后,它无法在任何浏览器中本地执行。我很好奇哪些部件可能被不当使用,或者我完全错过了什么,谢谢。的JavaScript不执行

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 

 
</head> 
 

 
<body> 
 

 
    <script type="text/javascript"> 
 
    var name = window.prompt("Hello, what is your name?"); 
 
    var choice = window.prompt("DVD or Blu-Ray?"); 
 
    var days = parseInt(window.prompt("How many days are you renting for?")); 
 

 

 
    if (choice == "DVD") { 
 
     double dvdcst = 2.99; 
 
     double dvdtot = dvdcst * days; 
 
     document.write("Name: " + name "<br />" 
 
     "Days renting: " + days + "<br />" 
 
     "Cost per day: " + dvdcst + "<br />" 
 
     "Total cost: " + dvdtot + "<br />"); 
 
    } else if (choice == "Blu-Ray") { 
 
     double blucst = 3.99; 
 
     double blutot = blucst * days; 
 
     document.write("Name: " + name + "<br />" 
 
     "Days renting: " + days + "<br />" 
 
     "Cost per day: " + blucst + "<br />" 
 
     "Total cost: " + blutot + "<br />"); 
 
    } 
 
    </script> 
 

 

 
</body> 
 

 
</html>

+4

打开浏览器的开发者工具和**读取错误消息**。 – Quentin

+1

不知道什么是规范的欺骗是的,但问题是你必须使用'+'在每行的末尾继续的字符串。因此''“租赁天数:”+天+“
”''变成''天租:“+天+”
“+' – Goose

+0

无关,但可能有帮助。 'dvdcst * days'和'blucst * days'可能会给出非金钱格式化的结果,例如'6.1'而不是'6.10'。 – Goose

回答

4

你有几个失踪+的。在第20行添加name"<br />"当你忘了一个,然后,用新行格式化时,您需要使用加号为好。

而且,double不存在在Javascript的事。您只能使用var(对于本地范围)或不带前缀来定义变量。

请尝试以下

<!DOCTYPE html> 
<html> 

    <head> 

    </head> 

    <body> 

      <script type="text/javascript"> 
      var name = window.prompt("Hello, what is your name?"); 
      var choice = window.prompt("DVD or Blu-Ray?"); 
      var days = parseInt(window.prompt("How many days are you renting for?")); 


      if (choice == "DVD") 
      { 
       dvdcst = 2.99; 
       dvdtot = dvdcst * days; 
       document.write("Name: " + name + "<br />"+ 
       "Days renting: " + days + "<br />"+ 
       "Cost per day: " + dvdcst + "<br />"+ 
       "Total cost: " + dvdtot + "<br />"); 
      } 

      else if (choice == "Blu-Ray") 
      { 
       blucst = 3.99; 
       blutot = blucst * days; 
       document.write("Name: " + name + "<br />"+ 
       "Days renting: " + days + "<br />"+ 
       "Cost per day: " + blucst + "<br />"+ 
       "Total cost: " + blutot + "<br />"); 
      } 


     </script> 


    </body> 

</html> 
+0

另请注意,您从变量中删除了“double”类型。 – Ken

+0

是的,真的,会解释。 – user6731765