I'm generating a dynamic drop down and trying to apply a value to it based on what's retrieved from another asynchronous call. At first I thought the value wasn't being applied because the dropdown wasn't fully created at the time I was setting the value...drop down is being created like so..and then applying the values to the form with the "GetOrderCallSucceeded" (a bunch of $('').val(value) calls, they work for everything accept for the dropdown)
function getEorder(id) {
var ReasonTypesPromise = makeAJAXcall("{}", "GetReasonTypes");
var getMedOrderPromise = makeAJAXcall("{'eMedOrderId':'" + id + "'}", "GetMedicalOrder");
$.when(ReasonTypesPromise, getMedOrderPromise).done(function (reasons, formdata) {
var items = JSON.parse(reasons.d), lr = JSON.parse(formdata.d);
for(var i in items) {
$('#ReasonForLifeVestDropdown')
.append($("<option></option>")
.attr("value", items[i].ReasonTypeID)
.text(items[i].Name));
if (items[i].ReasonTypeID == lr.ReasonTypeID) {
$('#ReasonForLifeVestDropdown').val(lr.ReasonTypeID);
}
}
GetOrderCallSucceeded(lr);
},
function (error, err) {
ajaxCallFailed(error);
});
}
However, I was testing
$('#ReasonForLifeVestDropdown').val(lr.ReasonTypeID);
$('#ReasonForLifeVestDropdown option[value=2]').attr('selected', 'selected');
simply by triggering the jquery onclick in a button. It wasn't working either.
HTML of dropdown and Button I was using for testing.
<div class="ui-block-a"><select id="ReasonForLifeVestDropdown"></select></div>
<div class="ui-block-a"><button data-role="button" onclick="$('#ReasonForLifeVestDropdown option[value=2]').attr('selected', 'selected');">click</button></div>
</div>
Aucun commentaire:
Enregistrer un commentaire