html - How to set the 'selected option' of a select dropdown list with jquery -
i have following jquery function:
$.post('getsalesrepfromcustomer', { data: selectedobj.value }, function (result) { alert(result[0]); $('select[name^="salesrep"]').val(result[0]); });
result[0]
value want set selected
item in select box. result[0]
equals bruce jones
.
the select box populated database query 1 of rendered html is:
<select id="salesrep" data-theme="a" data-mini="true" name="salesrep"> <option value=""> </option> <option value="john smith">john smith</option> <option value="bruce jones">bruce jones</option> <option value="adam calitz">adam calitz</option> <option>108</option> </select>
$('select[name^="salesrep"]').val(result[0]);
doesn't populate select box selected option. have tried $("#salesrep").val(result[0]);
without luck.
any appreciated.
so want selected / highlighted option in salesrep dropdown list bruce jones.
html be:
<select id="salesrep" data-theme="a" data-mini="true" name="salesrep"> <option value=""> </option> <option value="john smith">john smith</option> <option value="bruce jones" selected >bruce jones</option> <option value="adam calitz">adam calitz</option> <option>108</option> </select>
thanks again,
entire script
<script type="text/javascript"> $(document).ready(function () { $("#customer").autocomplete( { source: "get_customers", messages: { noresults: '', results: function() {} }, select: function( event, ui ) { var selectedobj = ui.item; $.post('getsalesrepfromcustomer', {data:selectedobj.value},function(result) { alert(result[0]); var selnametest="bruce koller"; $("#salesrep").val(selnametest); }); } }); }); </script>
rendered html is:
<select id="salesrep" data-theme="a" data-mini="true" name="salesrep"> <option value=""> </option> <option value="ryanlubuschagne">ryan lubuschagne</option> <option value="bruce jones">bruce jones</option> <option value="adam calitz">adam calitz</option> </select>
try :
$('select[name^="salesrep"] option[value="bruce jones"]').attr("selected","selected");
just replace option[value="bruce jones"]
option[value=result[0]]
and before selecting new option, might want "unselect" previous :
$('select[name^="salesrep"] option:selected').attr("selected",null);
you may want read : jquery specific option tag text
edit: using jquery mobile, link may provide solution : jquery mobile - set select/option values
Comments
Post a Comment