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

javascript - Why do some forms auto-submit on clicking the enter button and others do not? - Stack Overflow

programmeradmin4浏览0评论

Problem:

I never specify a submit button, as I use ajax to submit my forms, but some forms have an auto-submit feature regardless.

Background:

Throughout my application I have instances when clicking the enter button will auto-submit a form and other instances where it will do nothing. For example; there are times where I will need to capture the "enter" button to get an auto-submit but other times where it seems to just happen on it's own and I have not found the pattern. Except that the dynamically created forms seem to have the auto-submit feature but static ones do not? Does anyone else have a similar issue.

Example:

All of my forms have the submit button removed and I specify no target or action elements as seen below. All of the forms have their data transmitted through AJAX.

<form id="ajaxForm">
<input>
</form>
<button>ButtonOutSideForm</button>

Not looking for a way to prevent auto-submit

I already know about "onsubmit = return false" and e.preventDefault. I'm not looking for a way to disable auto-submit. My question is why does it's presence seem arbitrary. Thanks.

Problem:

I never specify a submit button, as I use ajax to submit my forms, but some forms have an auto-submit feature regardless.

Background:

Throughout my application I have instances when clicking the enter button will auto-submit a form and other instances where it will do nothing. For example; there are times where I will need to capture the "enter" button to get an auto-submit but other times where it seems to just happen on it's own and I have not found the pattern. Except that the dynamically created forms seem to have the auto-submit feature but static ones do not? Does anyone else have a similar issue.

Example:

All of my forms have the submit button removed and I specify no target or action elements as seen below. All of the forms have their data transmitted through AJAX.

<form id="ajaxForm">
<input>
</form>
<button>ButtonOutSideForm</button>

Not looking for a way to prevent auto-submit

I already know about "onsubmit = return false" and e.preventDefault. I'm not looking for a way to disable auto-submit. My question is why does it's presence seem arbitrary. Thanks.

Share asked May 1, 2013 at 2:09 Usman MutawakilUsman Mutawakil 5,2599 gold badges49 silver badges85 bronze badges 10
  • Just a thought, but have you looked at your tabindex on the page (both before and after the AJAX)? – Sablefoste Commented May 1, 2013 at 2:11
  • Sorry... of course tabindex. If you only have them on the AJAX loaded form, I would expect that a Return would plete the form. If you have them both on and off the AJAX, I am not sure what you might get. – Sablefoste Commented May 1, 2013 at 2:15
  • Also note that you don't actually need a submit button for the form to submit: tjvantoll./2013/01/01/… "So, in a form with no submit buttons, implicit submission will be done if only one input is present." – LouD Commented May 1, 2013 at 2:18
  • 1 Have you read stackoverflow./questions/925334/… – j08691 Commented May 1, 2013 at 2:25
  • 1 @Usman Mutawakil The point of the article is that there is no single standard. The spec says that there should be different functionality based on the types of fields you have in the form. In your case, that form only has a single input so it will auto-submit regardless of whether you have a submit button – LouD Commented May 1, 2013 at 13:55
 |  Show 5 more ments

1 Answer 1

Reset to default 6

As LouD pointed out. Some browsers will auto-submit if the form only contains one input element.Why does forms with single input field submit upon pressing enter key in input

发布评论

评论列表(0)

  1. 暂无评论