admin管理员组

文章数量:1134249

I have a long form that I've broken up into 6 steps. When the form is loaded, all of the steps are loaded, but only the first step is visible. The rest have CSS of display:none so they are hidden. As a step is completed and validated with Javascript, the current step is set to display:none and the new step is set to display:block. On the final step, the user submits the form. However, as expected, only the fields in display:block element on the page are submitted. All the completed fields in the elements with display:none are ignored.

Is there a way to submit the fields inside the display:none elements?

If not, is there another way to achieve the same effect?

I have a long form that I've broken up into 6 steps. When the form is loaded, all of the steps are loaded, but only the first step is visible. The rest have CSS of display:none so they are hidden. As a step is completed and validated with Javascript, the current step is set to display:none and the new step is set to display:block. On the final step, the user submits the form. However, as expected, only the fields in display:block element on the page are submitted. All the completed fields in the elements with display:none are ignored.

Is there a way to submit the fields inside the display:none elements?

If not, is there another way to achieve the same effect?

Share Improve this question edited Nov 29, 2011 at 22:23 Jon 2,9802 gold badges25 silver badges30 bronze badges asked Nov 29, 2011 at 22:08 Nick PetrieNick Petrie 5,59811 gold badges44 silver badges50 bronze badges 1
  • 14 What browser did you see this behaviour in? Browsers seem to submit form elements within display: none containers in all my tests. – tremby Commented Jun 9, 2014 at 22:19
Add a comment  | 

5 Answers 5

Reset to default 228

Set them to visibility:hidden and position:absolute instead. The fields will not be sent to the server with display:none, but will be with visibility:hidden. By also toggling "position" to "absolute" you should get the same visual effect.

Update This does not appear to be an issue anymore in any current browser (as of Nov of 2015). Fields are submitted even if display is set to 'none'. Fields that are 'disabled', however, will continue to not be submitted.

The HTML4, section 17.13.2, says explicitly that even hidden controls using display:none may be valid for submission.

https://www.w3.org/TR/html401/interact/forms.html

So if the browser ignores display:none then it is not fully HTML-capable. I recommend switching for a real browser.

If you find that your input is not submitted with display: none; and you want your element to not occupy space there is another option which I do not see mentioned anywhere.

It seems to work, but only if your element has no children.

display: contents;

Check the browser support as it is a newish CSS feature.

display:none - means the elements are hidden from view of the user. The html still sees them and the elements will still be submitted in the current version of Chrome, despite being hidden.

There are bugs in many older browsers where "display:none" removes an item in weird ways...like Webkit browsers pre-2012, where "display:none" on say a submit button, then pressing "return", would not submit the form field data to the server. There are also cases where "display:none" added to input fields of type "hidden" to hide them in old IE browsers fail to send that input data to the server.

The consensus should always be to NOT USE "display:none" to hide form field data or any field that is not normally displayed anyway, but which still needs to be seen by say a screen reader, submit form field data, or be read by search engines. I only use "display:none" when I truly want to remove something temporarily from the page but later enable it again.

Below is an alternative to "display:none" that just hides the item visually from the user and removes it completely from the page flow without hiding its content or data:

.hide {
    position: absolute !important;
    top: -9999px !important;
    left: -9999px !important;
    visibility: hidden !important;
}

本文标签: javascriptSubmit form fields inside displaynone elementStack Overflow