【问题标题】:Why does the function on my button not work?为什么我的按钮上的功能不起作用?
【发布时间】:2009-11-22 08:00:34
【问题描述】:

我正在编写一个表单来从框中获取数据并在单击“计算”按钮时进行计算。按钮通过函数cal()进行计算, 但它无法在countfield 框中显示结果。 如何在框中显示结果?还有,用“-”怎么命名函数?

<script type="text/javascript">
function cal()
{
   var hour = document.getElementById( "hourfield" ).value;
   var fee = document.getElementById( "feefield" ).value;
   var count = document.getElementById( "countfield" ).value;
   count = hour + fee;
}
</script>

【问题讨论】:

    标签: javascript function button


    【解决方案1】:

    更改以下内容:

    var count = document.getElementById( "countfield" ).value;
    count = hour + fee;
    

    到:

    document.getElementById( "countfield" ).value = hour + fee;
    

    这是因为对 count 变量的任何赋值只会改变它的引用,而不是表单中的字段。名称中不能有“-”,但可以使用“_”或“减号”。

    【讨论】:

      【解决方案2】:

      试试这个:

      <script type="text/javascript">
      function cal()
      {
      var hour = parseInt(document.getElementById( "hourfield" ).value);
      var fee = parseInt(document.getElementById( "feefield" ).value);
      
      document.getElementById( "countfield" ).value = hour + fee;
      }
      </script>
      

      【讨论】:

        【解决方案3】:

        您需要像这样将count 分配给countfield

        document.getElementById("countfield").value= count;
        

        【讨论】:

          猜你喜欢
          • 2021-11-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-13
          相关资源
          最近更新 更多