Populate item to dropdown by using ajax call and assign selected value

淺唱寂寞╮ 提交于 2019-12-13 04:33:38

问题


Please see my code below

$(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


回答1:


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();

or

dropdwonlist.val(item.AREA_ID).trigger('change');


来源:https://stackoverflow.com/questions/23572800/populate-item-to-dropdown-by-using-ajax-call-and-assign-selected-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!