使用ajax调用将项目填充到下拉列表并分配选定的值 [英] Populate item to dropdown by using ajax call and assign selected value
本文介绍了使用ajax调用将项目填充到下拉列表并分配选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请在下面查看我的代码
$(document).ready(function () {
readddl().done(function () {
$('#ddlAreas').val("51");
});
$("#plusBtn").bind("vclick", function () {
$('#ddlAreas').val("51");
});
});
function readddl() {
var df = $.Deferred();
var stateID = 18;
var dropdwonlist = $('#ddlAreas');
dropdwonlist.empty();
dropdwonlist.append($('<option></option>').val("--").html("Select Area"));
if (stateID != undefined && stateID != "--") {
// Send an AJAX request
$.getJSON(Config.Url + "Area?status=A&&stateID=" + stateID)
.done(function (data) {
// On success, 'data' contains a list of products.
$.each(data, function (index, item) {
// Add a list item for the product.
dropdwonlist.append($('<option></option>').val(item.AREA_ID).html(item.AREA_NAME));
});
}).fail(function (d) {
alert(d);
})
}
return df.promise();
}
我能够将项目填充到下拉菜单中.但是我不能将选择的值设置为下拉菜单.我也尝试通过单击加号按钮来设置选定的值,
I am able to populate item to the dropdown. But i cant set selected value to dropdown. I also try set the selected value by clicking the plus button and it work
请指导我解决方案.谢谢
Please guide me solution. Thanks
推荐答案
当您使用val()时,jQuery不会触发change事件,因此您必须自己进行操作才能使更改生效:
jQuery does not fire the change event when you use val(), so you have to do it yourself for the changes to take effect:
dropdwonlist.val(item.AREA_ID).change();
或
dropdwonlist.val(item.AREA_ID).trigger('change');
这篇关于使用ajax调用将项目填充到下拉列表并分配选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文