Skip to content Skip to sidebar Skip to footer

Preventing An Html Form Field From Being Submitted

I want to programmatically allow or hide a form field from being submitted in an HTML5 form. I thought I could just set its CSS display attribute to none. However, it still gets su

Solution 1:

Simply set disabled attribute for the form field, e.g.

<input name="test" value="test" disabled="disabled">

REF:http://www.w3.org/TR/html401/interact/forms.html#h-17.12.1

Solution 2:

Set the disabled property to the truth value true. It corresponds to the HTML attribute (“content attribute” in HTML5 parlance) with the same name but takes truth values. For example, assuming

<inputname=fooid=foo>

you could set, in JavaScript,

document.getElementById('foo').disabled = true;

This typically changes the appearance of the field, too, by making the background gray.

Setting display: none, or any CSS setting, has no impact on what gets submitted. CSS is for presentation (rendering), not functionality.

Post a Comment for "Preventing An Html Form Field From Being Submitted"