Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html option selected disabled cause select required not working

Tags:

html

<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?

like image 598
Benjamin W Avatar asked Feb 24 '17 03:02

Benjamin W


People also ask

Why required is not working in select tag?

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.

How do I select a disabled option in HTML?

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.).

How do you ensure a select form field is submitted when it is disabled?

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.

Can we use required in select tag?

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.


1 Answers

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

like image 119
JBallin Avatar answered Oct 05 '22 19:10

JBallin