2016-11-30 86 views
0

((这是家庭作业的一部分,但我没有要求任何人告诉我究竟写什么,所以挂在那里我认为这将很容易回答))为什么我不能编辑特定功能(javascript/html)以外的字段?

我有一个脚本模拟一个胡扯游戏。我将在最后发布它,但首先阅读其余的问题,因为大多数代码与我的问题无关。

我的工作是实施银行和投注系统。听起来很容易,但我卡住了。所以我的第一步是在结果表格中为银行写一栏。接下来,我把这个代码在play()函数:

bank = prompt("How much money is in your bank?"); 
document.getElementById("bankfield").value = bank; 

运行脚本,按下按钮调用播放(),窗口弹出,输入值,领域的变化。到现在为止还挺好。但我意识到,我不想在那里的代码,因为银行不应该每次掷骰子时重置。所以我剪切并粘贴了两行,直到脚本的开头,因此它是运行时发生的第一件事。 ......现在我被提示输入一个值之后,bankfield保持为空,我也得到this error

为什么这两条线工作得很好的发挥()函数,而不是公然在脚本?

我一直在搞这个很长一段时间,但我没有取得进展我想我在这里错过了一个概念,并认为这里有人可以启发我。谢谢!

这里是refence原来的游戏脚本:

 </style> 
    <script type = "text/javascript"> 

    // variables used to test the state of the game 
    var WON = 0; 
    var LOST = 1; 
    var CONTINUE_ROLLING = 2; 

    // other variables used in program 
    var firstRoll = true; // true if current roll is first 
    var sumOfDice = 0; // sum of the dice 
    var myPoint = 0; // point if no win/loss on first roll 
    var gameStatus = CONTINUE_ROLLING; // game not over yet 

    // process one roll of the dice 
    function play() 
    { 
     // get the point field on the page 
     var point = document.getElementById("pointfield"); 

     // get the status div on the page 
     var statusDiv = document.getElementById("status"); 
     if (firstRoll) // first roll of the dice 
     { 
      sumOfDice = rollDice(); 

      switch (sumOfDice) 
      { 
       case 7: case 11: // win on first roll 
       gameStatus = WON; 
       // clear point field 
       point.value = ""; 
       break; 
       case 2: case 3: case 12: // lose on first roll 
       gameStatus = LOST; 
       // clear point field 
       point.value = ""; 
       break; 
       default: // remember point 
       gameStatus = CONTINUE_ROLLING; 
       myPoint = sumOfDice; 
       point.value = myPoint; 
       firstRoll = false; 
      } // end switch 
     } // end if 
     else 
     { 
      sumOfDice = rollDice(); 

      if (sumOfDice == myPoint) // win by making point 
       gameStatus = WON; 
      else 
       if (sumOfDice == 7) // lose by rolling 7 
       gameStatus = LOST; 
     } // end else 

     if (gameStatus == CONTINUE_ROLLING) 
      statusDiv.innerHTML = "Roll again"; 
     else 
     { 
      if (gameStatus == WON) 
       statusDiv.innerHTML = "Player wins. " + 
       "Click Roll Dice to play again."; 
      else 
       statusDiv.innerHTML = "Player loses. " + 
       "Click Roll Dice to play again.";  

      firstRoll = true; 
     } // end else 
    } // end function play 

    // roll the dice 
    function rollDice() 
    { 
     var die1; 
     var die2; 
     var workSum; 

     die1 = Math.floor(1 + Math.random() * 6); 
     die2 = Math.floor(1 + Math.random() * 6); 
     workSum = die1 + die2; 

     document.getElementById("die1field").value = die1; 
     document.getElementById("die2field").value = die2; 
     document.getElementById("sumfield").value = workSum; 

     return workSum; 
    } // end function rollDice 
    // 
    </script> 

我很新的,一般JS和CS菜鸟,所以要温柔。另外我的第一个问题在这里被问到,如果我的格式不好或者其他问题很抱歉。

+0

将'