【问题标题】:Function call in JavaScriptJavaScript 中的函数调用
【发布时间】:2009-05-08 23:47:12
【问题描述】:

我很难过,我似乎无法调用这个简单的 Javascript 函数。 谢谢!

 <html>
 <head>


<script type="text/javascript">

    function increase()
    {
        alert(" the button was pressed");
    }

</script>         
 </head>

 <body>


 <form action="Test.html" method="post">

   <input type="submit" onclick="increase();" />

</form>   

 </body>
 </html>

【问题讨论】:

  • 好吧,你不会在我们能看到的任何地方调用它。这可以解释问题。

标签: javascript function call


【解决方案1】:

很难说你哪里出错了。看起来您只是在定义一个函数。这将在页面加载时运行增加函数:

<script type="text/javascript">
  function increase() {
    alert(" the button was pressed");
  }
  increase();
</script>

这将在按下按钮时运行该函数。

<script type="text/javascript">
  function increase() {
    alert(" the button was pressed");
  }
</script>
<button onclick="increase()">text</button>

听起来你才刚刚开始,这太棒了。我也建议买一本书。几年前,我读过 Jeremy Keith 的 DOM Scripting,还可以。另外,请尝试在网上四处寻找教程。

【讨论】:

  • 按钮必须在表单标签中吗?如果是的话,应该怎么看,谢谢
  • 不——不必是表单的一部分。
【解决方案2】:

试试这个:

<input type="button" id="buttonId">Button</input>

<script language="javascript">
    function increase() { alert(" the button was pressed"); } 

    document.getElementById("buttonId").onClick=increase;
</script>

【讨论】:

    【解决方案3】:

    你试过吗?您需要在脚本标签之间放置 JavaScript:

    <script type="text/javascript">
        function increase() { alert(" the button was pressed"); } 
    </script>
    

    【讨论】:

      【解决方案4】:

      除了 Praveen 所说的,我同意,还有很好的初学者教程 herehere

      【讨论】:

      【解决方案5】:

      在您的代码中,表单会在您单击按钮时提交。
      你需要做的是返回 false;这样表单就不会提交..

      <form action="#" method="POST">
        <input type="submit" onclick="return false; increase();">
      </form>
      

      【讨论】:

        猜你喜欢
        • 2012-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-06
        相关资源
        最近更新 更多