我想从JavaScript的下拉列表中选择一个值。
问题是选定的值没有显示。只有打开列表,我才能看到它被选中。
如何显示值文本?
代码:
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Jenis Pemeriksaan</label>
<div class="col-sm-12 col-md-10">
<asp:DropDownList runat="server" CssClass="custom-select2 form-control" ID="ddlJenisPeriksa" Width="100%" AppendDataBoundItems="true">
<asp:ListItem Value="" Text=""></asp:ListItem>
</asp:DropDownList>
</div>
</div>
function setDropDownList(elementRef, valueToSetTo) {
var isFound = false;
for (var i = 0; i < elementRef.options.length; i++) {
if (elementRef.options[i].text.toUpperCase() == valueToSetTo.toUpperCase()) {
elementRef.options[i].selected = true;
isFound = true;
}
}
alert(isFound );
if (isFound == false)
elementRef.options[0].selected = true;
}
$('a.editMappingKKP').click(function () {
$('#modalMappingKKP').modal('show');
var row = $(this).parents("tr");
var cols = row.children("td");
var id = $(this).data("id");
setDropDownList(document.getElementById('<%=ddlJenisPeriksa.ClientID %>'), $(cols[2]).text());
});