2014-07-06 131 views
-1

當我寫數組元素時,它不會顯示輸出,當我點擊發送和取消輸出將不會打印輸出將打印.... plese檢查我的查詢,如果它的錯誤...我認爲功能是停止工作,當我點擊按鈕,它沒有告訴我document.write不會顯示輸出

 <html> 
    <TITLE>Javascript Arrays</TITLE> 
    <BODY bgcolor="#F0F0F0"> 
    <h1>Implementation of Sort and Reverse Functions </h>  
    <h2>Enter number of Elements</h2> 
     <form name="Anonymous Ghost"> 
    <INPUT type="number" name="takeinput" id="valueField" size="30"> 
     <Input type="button" name="name1" value="Take Input" onClick="a()"> </form> 
    <form name="Anonymous Ghost"> 
    <Input type="button" name="name2" value="Ascending" onClick="b()"> 
    <Input type="button" name="name3" value="Descending" onClick="c()"> 
     </form> 

      </BODY> 
     <SCRIPT> 
    function a() { 
     var val = parseInt(document.getElementById('valueField').value) 
      var words = new Array(val); 
      for (k = 0; k < words.length; k = k + 1) { 
    words[k] = window.prompt("Enter String # " + k, ""); 
      } 
      } 

     function b() { 
     words.sort() ; 
     document.write("SORTED WORDS:" + "<BR>") ; 
     for (k = 0 ; k < words.length ; k = k + 1) { 
     document.write(words[ k ] + "<BR>") ; 
     } 
     } 

     function c() { 
     words.sort() ; 
     words.reverse() ; 
     document.write("SORTED WORDS:" + "<BR>") ; 
     for (k = 0 ; k < words.length ; k = k + 1) { 
     document.write(words[ k ] + "<BR>") ; 
     } 
     } 
     </SCRIPT> 
     </HEAD> 
     <BODY> 
     </BODY> 
      </HTML> 
+0

'words'是你的'a'函數的局部變量。它在'b'和'c'中都沒有定義。所以一個簡單的解決辦法是將'單詞'定義爲一個全局變量。 – Arnauld

+0

你是否像這樣_really_格式化你的代碼?多可怕的一團糟! –

+0

請格式化您的代碼。您的編輯應該有一個選項。 –

回答

0

使用功能a()範圍界定words所以它不是指出範圍的訪問。 修改代碼,在全局範圍內定義words和刪除功能a()var關鍵字所以它不會覆蓋全局words

您的代碼應該是這樣的:

<html> 
    <head> 
     <title>Javascript Arrays</title> 
    </head> 
    <body bgcolor="#F0F0F0"> 
     <h1>Implementation of Sort and Reverse Functions </h1>  
     <h2>Enter number of Elements</h2> 
     <form name="Anonymous Ghost"> 
     <INPUT type="number" name="takeinput" id="valueField" size="30" /> 
     <Input type="button" name="name1" value="Take Input" onClick="a()" /> 
     </form> 
     <form name="Anonymous Ghost"> 
     <Input type="button" name="name2" value="Ascending" onClick="b()" /> 
     <Input type="button" name="name3" value="Descending" onClick="c()" /> 
     </form> 
    </body> 
    <script> 
     var words; 
     function a() { 
      var val = parseInt(document.getElementById('valueField').value); 
      words = new Array(val); 
      for (k = 0; k < words.length; k = k + 1) { 
       words[k] = window.prompt("Enter String # " + k, ""); 
      } 
     } 

     function b() { 
      words.sort() ; 
      document.write("SORTED WORDS:" + "<BR>") ; 
      for (k = 0 ; k < words.length ; k = k + 1) { 
       document.write(words[ k ] + "<BR>") ; 
      } 
     } 

     function c() { 
      words.sort() ; 
      words.reverse() ; 
      document.write("SORTED WORDS:" + "<BR>") ; 
      for (k = 0 ; k < words.length ; k = k + 1) { 
       document.write(words[ k ] + "<BR>") ; 
      } 
     } 
    </script> 
    </html> 

PS:您可以使用開發工具您的瀏覽器檢查Javascripts控制檯中的錯誤。

PS2:保持您的代碼整潔。