在enter-key-press上执行

时间:2013-11-14 11:11:46

标签: javascript html html5

我有这段代码

<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>  

当您单击Go for it-Button时,正在加载whatever.php#anchor。如何更改输入标记的元素以执行与单击按钮相同的cA('whatever.php#anchor')

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

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

PS:

在一个表单中包装all不起作用,因为页面的结构是

<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>    

1 个答案:

答案 0 :(得分:2)

你可以试试这个:

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

查看this帖子了解详情。