2013-10-23 91 views
0

這是我的程序在Java腳本我沒有出現任何事情,當我按搜索,但功能按鈕按()工作完全孤獨,但是當我使用代碼的形式從文本中取出句子它不起作用。但是當我嘗試在許多地方打印結果時,我注意到每一件事情都是確定的,在第一個for循環中直到現在我不知道原因,請任何幫助代碼:爲什麼JavaScript程序不運行

 <?xml version = "1.0" encoding = "utf-8"?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

    <!-- Fig. 11.5: SearchingStrings.html --> 
    <!-- String searching with indexOf and lastIndexOf. --> 
    <html xmlns = "http://www.w3.org/1999/xhtml"> 
     <head> 
      <title> 
      Searching Strings with indexOf and lastIndexOf 
      </title> 
      <script type = "text/javascript"> 
      <!-- 


      function buttonPressed() 
      { 

       // var searchForm = document.getElementById("searchForm"); 
       var inputVal = document.getElementById("inputVal").value; 
       var arr = inputVal.split(" "); 

       arr.sort(); 

    for(var i = 0; i< arr.length; ++i) { 
     var num = arr[i]; 
     counts[num] = (counts[num] ? counts[num] : 0) + 1 ; 

        document.writeln(arr[i]+"</br>"); 
    } 

    document.writeln("xxxxxxxxxxxxxxafter forrrrrrrr"); 
    document.writeln("<table border=\"4\">"); 
    document.writeln("<caption><h3>Search's Results: <h3/></caption>"); 
    for (var i in counts) { 
     document.writeln("<tr><td>"+i+" :</td><td>"+counts[i]+"</td></tr>"); 
    } 
    document.writeln("</tbody></table>"); 

      } // end function buttonPressed 
      // --> 
      </script> 
     </head> 
     <body> 
    <form action = ""> 
      <h1>The string to search is:<br /> 
       </h1> 
      <p>Enter Sentence You want To Find The Occurrence Of Each Word In It : 
      <input id = "inputVal" type = "text" /> 
      <input name = "search" type = "button" value = "Search" 
        onclick = "buttonPressed()" /><br /></p> 


      </form> 
     </body> 
    </html> 

回答

2

看起來counts變量在您的腳本中是未定義的。所以申報,然後嘗試。

JsFiddle Demo

+0

哦,上帝啊,這是理由謝謝 –