【问题标题】:Javascript won't run on php pageJavascript 不会在 php 页面上运行
【发布时间】:2009-04-25 15:58:56
【问题描述】:

我有一个 php 页面,我有一些 javascript 代码来运行一些字段。我很好地从我拥有的工作测试中复制并修改了一些代码以适应。好吧,它不起作用,我似乎无法工作。有什么明显的我遗漏了,还是有其他原因导致它没有运行?

 <?php

 //server connection info

 ?>
 <html>
 <head>
 <title>Survey</title>
 <link rel="stylesheet" type="text/css" href="styles.css" />

       <script type="text/javascript">
           function Total()
           {

               var a=document.getElementById("a").value;
               var b=document.getElementById("b").value;
               var c=document.getElementById("c").value;
               var d=document.getElementById("d").value;

               a=parseInt(a);
               b=parseInt(b);
               c=parseInt(c);      

               var total=a+b+c;



               document.getElementById("total").value=total;


           }


       </script>


 </head>


 <body>
 <h1>QUALITY OF LABOR SURVEY</h1>
 <p />
 <h2>ABOUT YOUR COMPANY</h2>


 <div class="Wrapper">

     <form id="Main" method="post" action="Process.php">

         <div class="Question">
              1. In what state and county is your business located? (click below)
         </div>
         <div class='answer'>
         <?php
             $tsql = "select
                StateCountyID,
                State,
                County
                from dbo.StateCounty
                where State='MO'
                      and Active='True'
                order by State";

             $tsql2 = "select
                StateCountyID,
                State,
                County
                from dbo.StateCounty
                where State='IL'
                      and Active='True'
                order by State";


             /* Execute the query. */
             $stmt = sqlsrv_query( $conn, $tsql);
             if ( $stmt )
             {

                 echo "<span><select name='ListMO'>";
                 echo "<option value='0'>MO-County</option>";

                 while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))
                 {

                     echo "<option value='".$row[0]."'>";
                     echo $row[2];
                     echo "</option>";

                 }

                 echo "</select></span>";

             }
             else
             {
                  echo "Error in statement execution.\n";
                  die( print_r( sqlsrv_errors(), true));
             }


             $stmt2 = sqlsrv_query( $conn, $tsql2);
             if ( $stmt2 )
             {

                 echo "<span><select name='ListIL'>";
                 echo "<option value='0'>IL-County</option>";

                 while( $row = sqlsrv_fetch_array( $stmt2, SQLSRV_FETCH_NUMERIC))
                 {
                     echo "<option value='".$row[0]."'>";
                     echo $row[2];
                     echo "</option>";

                 }

                 echo "</select></span>";

             }
             else
             {
                 echo "Error in statement execution.\n";
                 die( print_r( sqlsrv_errors(), true));
             }
        ?>
           </div>



         <table width="700px">
           <tr>
             <td style="font-size: 1.2em; font-weight: bold;">
                 ABOUT YOUR EMPLOYMENT
             </td>
             <td style="font-weight: bold;">
                 (Exclude Temporary Employees Throughout Survey)

             </td>
           </tr>

         </table>

         <p />
         <b>Please estimate the following:</b>







         <p />




         <div class="Question">
              4. Number of Full-Time Hourly Employees (Eligible for Full-Time Benefits)
         </div>

         <div class="Answer">
              <input type="text" id="a" value="0" onchange="Total();" />
         </div>

         <div class="Question">
              5. Number of Part-Time Hourly Employees(Not Eligible for Full-Time Benefits)
         </div>

         <div class="Answer">
              <input type="text" id="b" value="0" onchange="Total();" />
         </div>

         <div class="Question">
              6. Salaried Employees
         </div>

         <div class="Answer">
              <input type="text" id="c" value="0" onchange="Total();" />
         </div>





         <div class="Question">
              7. Is this your current number of employees?  If not, change responses to 4, 5, and 6.
         </div>

         <div class="Answer">
              <input type="text" id="total" value="0" onchange="Total();" />
         </div>

【问题讨论】:

    标签: php javascript html


    【解决方案1】:
    var d=document.getElementById("d").value;
    

    产生错误,因为您似乎没有id="d" 的元素

    【讨论】:

    • 当我看的时候我以为它说另一个是第一个耸耸肩
    • 检查您的问题的排序。让它按投票排序。
    【解决方案2】:

    我还想提出一些建议来改进您的代码。

    调试javascript

    • firefox 扩展 firebug 绝对是调试 javascript 的必备工具。

    https://addons.mozilla.org/en-US/firefox/addon/1843

    编写 Javascript

    • 将 javascript 放在一个单独的文件中,例如:myjavascript.js 并从您的 html 页面中包含该文件。这将减少您的页面重量,并且您将有更清晰的代码分离。
    • 使用 javascript 框架来帮助您生成更好的 javascript,例如 jquery 或 yui(雅虎用户界面库)。

    http://jquery.com/
    http://developer.yahoo.com/yui/

    编写 PHP

    • 通过将 PHP 放在单独的文件中并包含它来更好地分离 PHP/HTML。例如:

    http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html/

    【讨论】:

      【解决方案3】:

      需要检查的几件事。

      • 不是在所有浏览器中都可以使用吗?
      • 您是否确认确切的代码在单独的文件中有效
      • 您是否尝试将脚本移至页面底部?

      您还应该在 Firefox 中使用调试器,甚至可以为 Firefox 找到像 Firebug 这样的工具。对发现此类问题有很大帮助。

      【讨论】:

        【解决方案4】:

        请尽量缩短您的问题!隔离它!

        我也强烈推荐使用Firebug

        你很快就会找到答案:)

        【讨论】:

          【解决方案5】:

          您的 HTML 中没有元素“d”。如果您注释掉该行:

          var d=document.getElementById("d").value;
          

          应该可以正常使用

          【讨论】:

          • 谢谢。我以为我把那条线去掉了,但显然我错了。现在可以了,耶!
          • 我会说 SilentGhost 值得更多检查(特别是因为他快了 3 分钟),但是哦。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-19
          • 1970-01-01
          • 2022-06-18
          • 2023-03-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多