简体   繁体   English

为什么javascript程序不运行

[英]why the javascript program doesn't run

this is my program in java script i don't appear any thing when i press search but the function button-pressed() is worked perfectly lonely but when i using the code with form to take the sentence from the text it doesn't work .but when i try to print the result in many places i notice that every thing is OK else in the first for loop until now i don't know the reason please any help there is the code : 这是我在java脚本中的程序我没有出现任何事情,当我按下搜索但功能按钮按下()工作完全孤独,但当我使用表单的代码从文本中取出句子时它不起作用。但是当我尝试在许多地方打印结果时,我注意到在第一个for循环中的每个东西都没问题,直到现在我不知道原因请求任何帮助有代码:

        <?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>

It looks that counts variable is undefined in your script. 看起来counts变量在您的脚本中是未定义的。 So declare it and then try. 所以宣布它,然后尝试。

JsFiddle Demo JsFiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM