0

I have a jQuery EasyUI combobox on a page like this:

<select id="subject_selection" class="easyui-combobox">
    <option value="math">Math</option>
    <option value="physics">Physics</option>
    <option value="chemistry">Chemistry</option> 
    <option value="biology">Biology</option>
</select>

I'm trying to set the index of the default selected value to -1 if there is no value set, I tried to set the attribute selectedIndex to -1 like selectedIndex="-1" for the select element, but that still does not work. I don't want to give the user the ability to chose and empty value in the combobox, therefore I don't want to add this:

<option value=""></option>

In my combobox. Does anybody have any idea how I can achieve this?

Thank you

Alex
  • 34,899
  • 5
  • 77
  • 90
user765368
  • 19,590
  • 27
  • 96
  • 167

3 Answers3

0

"You're trying to fit a square peg in a round hole."

There is no such thing as a -1 index, as you can't select something that doesn't exist. Add the empty option and prevent the user from selecting it with JavaScript.

Alex
  • 34,899
  • 5
  • 77
  • 90
0

Instead of setting selectedIndex to -1, you can disable combobox or set alert message if there is no items in combobox.

SanketS
  • 963
  • 1
  • 13
  • 36
0

you can try something like this if you want math as your default value

<select id="subject_selection" class="easyui-combobox">
   <option value="math" selected="selected">Math</option>
   <option value="physics">Physics</option>
  <option value="chemistry">Chemistry</option> 
  <option value="biology">Biology</option>
</select>

just put selected="selected" on the option that you want to be selected as default

or you can do like this

<select id="subject_selection" class="easyui-combobox" data-options="onLoadSuccess : function(){$(this).combobox('setValue','biology');}">
  <option value="math">Math</option>
  <option value="physics">Physics</option>
  <option value="chemistry">Chemistry</option> 
  <option value="biology">Biology</option>

just change the value as you want which is as the default one

just test the code with link below

http://jsfiddle.net/kapasaja/3v666ncg/

http://jsfiddle.net/kapasaja/few3xgtj/

Hilarius L. Doren
  • 757
  • 1
  • 12
  • 29