我有这个代码

<script>
function cA( url )
{
    document.myform.action = url;
}
</script>

<input type="whatever" name="whatever" id="whatever" />
<button class="whatever-button" id="defaultAction" value="Go for it" name="whatever" type="submit" onClick="cA('whatever.php#anchor')" >Go for it
</button>


当您单击开始按钮时,正在加载whatever.php#anchor。我如何更改输入标签的元素以使其执行与单击按钮相同的cA('whatever.php#anchor')

因此,当用户在该字段中按下Enter键时,应该加载站点whatever.php#anchor,或更具体地说,应执行cA('whatever.php#anchor')

该页面上有更多按钮,因此将一个按钮设置为默认按钮不起作用。

PS:

将所有内容包装成一种形式不起作用,因为页面的结构是

<form>
    <script>
    function cA( url )
    {
        document.myform.action = url;
    }
    </script>

    <input type="whatever1" name="whatever1" id="whatever1" />
    <button class="whatever1-button" id="defaultAction1" value="Go for it" name="whatever1" type="submit" onClick="cA('whatever.php#anchor1')" >Go for it
    </button>

    <input type="whatever2" name="whatever2" id="whatever2" />
    <button class="whatever2-button2" id="defaultAction2" value="Go for it" name="whatever2" type="submit" onClick="cA('whatever.php#anchor2')" >Go for it
    </button>

    <input type="whatever3" name="whatever3" id="whatever3" />
    <button class="whatever3-button" id="defaultAction3" value="Go for it" name="whatever3" type="submit" onClick="cA('whatever.php#anchor3')" >Go for it
    </button>
    ...
</form>

最佳答案

您可以尝试以下方法:

<input type="whatever" name="whatever" id="whatever" onkeydown="if (event.keyCode == 13) {cA('whatever.php#anchor');}" />


查看this帖子以获取更多信息。

关于javascript - 按Enter键执行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19976156/

10-11 23:25