简体   繁体   English

在enter-key-press上执行

[英]Execute on enter-key-press

I have this code 我有这个代码

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

When you click the Go for it-Button, whatever.php#anchor is being loaded. 当您单击Go for it-Button时,正在加载whatever.php#anchor How do I need to change the input tag's elements for it to execute the same cA('whatever.php#anchor') as the button click? 如何更改输入标签的元素,以便在按钮单击时执行相同的cA('whatever.php#anchor')

So when a user presses enter in the field, the site whatever.php#anchor should be loaded or more specifically, cA('whatever.php#anchor') be executed. 因此,当用户在字段中按Enter键时,应该加载站点whatever.php#anchor或者更具体地说,执行cA('whatever.php#anchor')

The page has more buttons, so making one button the default button does not work. 该页面有更多按钮,因此将一个按钮设为默认按钮不起作用。

PS: PS:

Wrapping all in one form does not work as the page's structure is 将所有内容包装在一个窗体中不起作用,因为页面的结构是

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

You can try this: 你可以试试这个:

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

Check this post for more information. 查看帖子了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM