<select name="status" required>
<option selected disabled>status</option>
I have set required attribute in html select.
My first option set selected disabled for display text.
However it will cause required attribute not working.
anyone know how to solve this problem?
The required attribute is there in the HTML select element. If it is not working for you, then try checking back the “first value” field. You need an empty value there.
The disabled attribute is a boolean attribute. When present, it specifies that an option should be disabled. A disabled option is unusable and un-clickable. The disabled attribute can be set to keep a user from selecting the option until some other condition has been met (like selecting a checkbox, etc.).
Instead of disabling the select , disable all of the option s and use CSS to gray out the select so it looks like its disabled. Add a click event handler to the submit button so that it enables all of the disabled dropdown menus before submitting the form.
The required attribute of the <select> element is to let users know that the <select> drop-down is required and need to be submitted before the form is submitted. If you won't select the value from the drop-down and try to submit the form, it won't submit and a warning would be visible on the web page itself.
You need to set the value
attribute of option
to an empty string:
<select name="status" required>
<option selected disabled value="">status</option>
</select>
select
will return the value
of the selected option
to the server when the user presses submit
on the form
. An empty value
is the same as an empty text
input → raising the required
message.
w3schools:
The value attribute specifies the value to be sent to a server when a form is submitted.
Example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With