2012-02-18 153 views
1

如果你有一个正常的形式:做一个链接“进入,点击”

<form method='post' action='#'> 
<input type='text' name='#' /> 
<input type='submit' value='Submit /> 
</form> 

然后你就可以在输入栏填写,然后按回车键。不过,我使用的是链接做我的工作如此:

<form method='post' action='#'> 
<input type='text' name='#' /> 
<a href='#' class='button' onclick='get_form(this).submit();'>Submit</a> 
</form> 

这工作就像一个魅力,但我希望它继承了“进入,点击”是正常的提交字段。有可能通过一些JavaScript?

+0

这可能帮助:http://stackoverflow.com/questions/477691/html-submitting-a-form-b​​y-pressing-enter-without-a-submit-button – TheShellfishMeme 2012-02-18 16:04:04

+1

你为什么要当一个按钮使用链接已经做了你想做的事情?如果出于风格目的,您可以使用'

回答

5
$('#your_input_textfeild_id').keypress(function(e) { 
    //check if enter is pressed 
    if(e.keyCode == 13) { 


    //click your link 
     $('#your_submit_link_id').click(); 



    } 
    }); 
2

试试这个:

<script> 
    document.getElementById('TEXTBOXID').onkeypress = function(e){ 
     var evt = e || window.event; 
     if(evt.keyCode == 13){ 
      get_form(this).submit(); 
     } 
    } 
</script> 
1

你可以试试这个

document.getElementById('my_input').onkeypress = function(e) 
{ 
    var event = e || window.event; 
    if(event.keyCode == 13 && this.value!="") 
    { 
    form = this.parentNode; 
    if(form.tagName.toLowerCase() == "form") 
    { 
     form.submit(); 
    } 
    } 
} 

You have to add an id to your text box and replace my_input with it. 
1

我认为这样做的正确方法是实际包括

<input type="submit" > 

你然后可以隐藏风格。或者,只需将提交元素设置为您想要的样式即可。我的理由是浏览器查找提交元素,这就是输入密钥如何分配以提交表单。我总觉得最好让浏览器处理这种事情,而不是使用JavaScript来捕捉按键事件。