最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - making a submit button respond to hitting enter and click - Stack Overflow

programmeradmin3浏览0评论

I see on many sites that the enter key will submit a form but you could also click on the button to submit. Is there a trick to this? Or can a button have two different types of functions attached?

I see on many sites that the enter key will submit a form but you could also click on the button to submit. Is there a trick to this? Or can a button have two different types of functions attached?

Share Improve this question asked Nov 3, 2011 at 14:28 SamSam 2,3479 gold badges39 silver badges53 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

If you have a properly marked up form with an input element of type=submit as your submit button, the enter button should submit the form by default (when focused in an input element of type=text). Clicking works as well.

Any processing code would go on the form's "onsubmit" event instead of the button's "onclick" event. Additionally, you can return false to cancel the form submission (for example, if form validation fails).

My dear there is nothing tricky. Just put the type of the button as submit and your will achieve what you are wanting. See This

After UPDATE

When there is a form tag in your html page, enter button always submit the form. If you have made your own button having type="button" even then you can submit form onclick it.

<input type="button" onclick="submitform();" value="Button">

and here is your javascript function

function submitform()
 {
   document.forms["myform"].submit();
 }

Hence your both cases can be achieved

发布评论

评论列表(0)

  1. 暂无评论