Empty selects, multiple returns null and the single returns the first option
I have two selects, one "regular" and one multiple. If nothing is selected in the multiple one, then it returns "null", the single select returns the first option if I don't make a choice.
The thing is that I need the single select to return "null" if the first开发者_开发知识库 (empty) option is selected. Is this possible to define somehow, either via jQuery or something else.
Do you understand what I mean?
Cheers!
Edit:
The code is straight forward, just two selects:
<select id="single">
<option>Make your selection</option>
<option value="sel1">Test 1</option>
<option value="sel2">Test 2</option>
<option value="sel3">Test 3</option>
<option value="sel4">Test 4</option>
</select>
<select id="multi" multiple="multiple">
<option>Make your selection</option>
<option value="mult1">Test 1</option>
<option value="mult2">Test 2</option>
<option value="mult3">Test 3</option>
<option value="mult4">Test 4</option>
</select>
If you don't interact with the multiple select it will return "null". I would like the single one to return "null" if the user doesn't interact with it and the first option remains selected.
This is due to browser behavior: first, in single select element the first item is selected by default.
Second, when the value
attribute of the <option>
tag is omitted, the browser will assign the text of the <option>
as the value.
The most simple solution will be to add empty value to your first item in the list:
<option value="">Make your selection</option>
This way you don't need to make any changes in your JS code.
精彩评论