0

i have a html select which has multiple select option enabled.I want to to show values selected which are in database.

eg : from database i will get values like this 2009 or 2009,2010 or 2009,2010,2012..i mean it may contain ',' or may not..

and my html select is

<select id="year_selected" tabindex="14" name="year_selected" multiple="multiple">
<option>2009</option>
<option>2010</option>
<option>2011</option>
<option>2012</option>
</select>

How do i do it jquery

Mahajan344
  • 2,492
  • 6
  • 39
  • 90
  • possible duplicate of [JQuery multiselect - Set a value as selected in the multiselect dropdown](http://stackoverflow.com/questions/6966260/jquery-multiselect-set-a-value-as-selected-in-the-multiselect-dropdown) -- ignore the call to `.multiselect` though, that seems to be some plugin. – Felix Kling Aug 15 '13 at 15:49
  • I don't think your question is actually clear. What do you mean with "show selected which are in database"? Is your workflow `select one value - check db` or `retrieve from db - populate select - select one`? – Samuele Mattiuzzo Aug 15 '13 at 15:50
  • kindly see my below comment ..hope that clears my problem.. – Mahajan344 Aug 15 '13 at 16:07

3 Answers3

0

On the options that are to be selected add the html attribute selected and you should be in business.

David MacCrimmon
  • 966
  • 1
  • 6
  • 19
  • i am using MVC.. so the value i am getting on page load is from model property and its value i am getting from database is like 2009 or 2009,2010 or 2009,2010,2012... so if database have 2009 then only this value from select should be selected.. same if database has values 2009,2010 then only 2009 and 2010 should be selected – Mahajan344 Aug 15 '13 at 16:05
0

send an ajax request to sever side and build your select with selected option, where you can easily get values from database and return that html as an ajax response

usman allam
  • 274
  • 1
  • 5
0

Thanks guy for your help..i got solution..its working perfect..

 var yeararray = str_year_selected.val().split(",");
            for (var i in yeararray)
            {
                var val = yeararray[i];                    
                year_selected.find('option:[value=' + val + ']').attr('selected', 1);
            }
Mahajan344
  • 2,492
  • 6
  • 39
  • 90