javascript - setting selected value inside combobox using its name not index -
if i'm rendering combobox this
$('#mycombo').html('<option value=""></option>'); $.each(mydata, function (i, v) { $('#mycombo').append('<option value="' + v.personid+ '">' + v.personname + '</option>'); });
how can set selected value inside combobox if v.personname == 'john'
p.s. know can set value using it's index
$('#mycombo').prop('selectedindex', 2);
but in case don't know index, know name.
one simple answer
$('#mycombo').html('<option value=""></option>'); $.each(mydata, function (i, v) { if (v.personname== 'john') $('#mycombo').append('<option selected value="' + v.personid+ '">' + v.personname + '</option>'); else $('#mycombo').append('<option value="' + v.personid+ '">' + v.personname + '</option>'); });
Comments
Post a Comment