Hello,

I have an select box:

<select id="selectBox"> 
<option value="0">Number 0</option> 
<option value="1">Number 1</option> 
<option value="2">Number 2</option> 
<option value="3">Number 3</option> 
<option value="4">Number 4</option> 
<option value="5">Number 5</option> 
<option value="6">Number 6</option> 
<option value="7">Number 7</option> 
</select> 

I'd like to set one of the options as "selected" based on it's selected index.

For example, if I am trying to set "Number 3", I'm trying this:

$('#selectBox')[3].attr('selected', 'selected'); 

But this doesn't work. How can I set an option as selected based on it's index using jQuery?

Thanks!

Answers,

You've selected the <select>, not the <option>s inside of it.

$('#selectBox option')[3].attr('selected', 'selected') 

原文轉貼自:http://stackoverflow.com/questions/1280499/jquery-set-select-index 

 


    全站熱搜

    Frank 發表在 痞客邦 留言(0) 人氣()