onclick按钮输入在Chrome中无效

时间:2013-10-15 02:43:52

标签: javascript html google-chrome

Javascript代码:

function doClick()
{
  alert("clicked");
}

HTML code:

<div >
<table border="2" cellspacing="0" cellpadding="0" class="TextFG" style="font-family:Arial; font-size:10pt;">
<tr>
<td width="100%">
  <table border="0" style="font-family:Arial; font-size:10pt;" cellspacing="0" cellpadding="3">
    <tr>
      <td>&nbsp;Find:</td>
      <td nowrap>
        <input type="text" id="Find_Text" name="Find_Text" size="7" >&nbsp;<input type="checkbox" name="Find_ExactMatch" value="1" checked>
      </td>
      <td colspan="2" style="cursor:default" nowrap >Exact Match</td>
    </tr>
    <tr>
      <td>&nbsp;In:</td>
      <td>
        <select name="Find_DataFile">
          <option value="1">Stage 1</option>
          <option value="2">Stage 2</option>
        </select>
      </td>
      <td align="center" valign="top">
        <input type="submit" value="Go" onclick="doClick();"/>
      </td>
      <td align="center"><font class="DataFG">F2<font></td>
    </tr>
  </table>
</td>
</tr>
</table>
</div>

大家好,

如上所述的简单代码不会在Chrome(最新)中触发,但会在IE8和IE10下触发。 我做错了吗?。

感谢任何帮助 感谢

在我上面提供的代码中逐步执行每行(其中还包括一个服务器请求来填充名为“Find_DataFile”的下拉列表,其代码我无法提供)我终于找到导致对象无法生成的罪魁祸首定义的错误(因为它的id未定义,我正在调用getElementById将其分配给临时对象)。现在所有的IE8 IE10 Chrome和Safari都在使用。谢谢你们花时间和精力帮我找到解决方案。非常感谢。我现在可以正常呼吸了哈哈!

3 个答案:

答案 0 :(得分:1)

提交按钮是一种特殊类型的按钮,仅用于&lt;形式&gt;标签。它运行您在其所属表单的“onsubmit”属性中指定的任何函数。请参阅Here,了解提交按钮如何与javascript和“onsubmit”形式进行交互。如果以这种方式连接,您将能够获得所需的效果。所以要特别注意代码的FORM标记......

<body>
    <div>
        <form name="myForm" action="http://www.google.com" onsubmit="return doClick();" method="post">
            <table border="2" cellspacing="0" cellpadding="0" class="TextFG" style="font-family:Arial; font-size:10pt;">
                <tr>
                    <td width="100%">
                      <table border="0" style="font-family:Arial; font-size:10pt;" cellspacing="0" cellpadding="3">
                        <tr>
                          <td>&nbsp;Find:</td>
                          <td nowrap>
                            <input type="text" id="Find_Text" name="Find_Text" size="7" >&nbsp;<input type="checkbox" name="Find_ExactMatch" value="1" checked>
                          </td>
                          <td colspan="2" style="cursor:default" nowrap >Exact Match</td>
                        </tr>
                        <tr>
                          <td>&nbsp;In:</td>
                          <td>
                            <select name="Find_DataFile">
                              <option value="1">Stage 1</option>
                              <option value="2">Stage 2</option>
                            </select>
                          </td>
                          <td align="center" valign="top">
                            <input type="submit" value="Go" />
                          </td>
                          <td align="center"><font class="DataFG">F2<font></td>
                        </tr>
                      </table>
                    </td>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>

然后是javascript:

function doClick()
{
alert("I've been clicked");
}

这对我有用:)

答案 1 :(得分:0)

为您的样品。它应该工作。

您应该确定两件事:

  1. 单击该按钮时,请确保Javascript代码具有 已装载。
  2. 您的Javascript代码不得有任何语法错误。
  3. 如果按钮位于表单内,则单击后可以提交表单。

答案 2 :(得分:0)

提交按钮已具有默认功能。它不会在许多浏览器中触发onclick功能。尝试将按钮类型更改为“按钮”并尝试使用javascript提交表单。

相关问题