在jqueryajax中使用回调来设置在下拉菜单中选择的选定文本

use call back in jquery ajax to set a selected text selected in drop down

本文关键字:选择 文本 下拉菜单 jqueryajax 回调 设置      更新时间:2023-09-26

我正在填充一个国家/地区/州和城市下拉列表,但我可以填充下拉列表中的数据,但我想要的是设置我从数据库请求的选项的选定值,但我无法做到这一点,我所做的是使用回调函数。也许可以在不使用回电的情况下完成。下面是我的代码。var状态;

function func(state) {
    //alert("callback " + state);
    $('#state option[value="' + state + '"]').prop('selected', 'selected');
}
$.ajax({
    url: 'http://localhost:3000/getcountrylist',
    type: 'GET',
    success: function (data, textStatus, jqXHR) {
        var countryDropDown = $('#country');
        var citizenCountryDropDown = $('#citizencoutnry');
        //alert("In country list");
        $.each(data, function (index, result) {
            countryDropDown.append(
            $('<option/>', {
                value: result.countrycode,
                text: result.countryname
            }));
            citizenCountryDropDown.append(
            $('<option/>', {
                value: result.countrycode,
                text: result.countryname
            }));
        });
        $.ajax({
            url: 'http://localhost:3000/editempform?userid=' + split,
            type: 'GET',
            success: function (data, textStatus, jqXHR) {
                $.each(data, function (index, result) {
                    //$('#country option[value="PK"]').prop('selected', 'selected');
                    //$('select[name="country"] option[value="PK"]')
                    $("#country").find("option").filter(function (index) {
                        return result.country === $(this).text();
                    }).prop("selected", "selected");
                    $.ajax({
                        url: 'http://localhost:3000/getstatelist',
                        type: 'GET',
                        data: {
                            'countrycode': $("#country").val()
                        },
                        success: function (data, textStatus, jqXHR, func) {
                            var stateDropDown = $('#state');
                            $.each(data, function (index, result) {
                                stateDropDown.append(
                                $('<option/>', {
                                    value: result.state,
                                    text: result.state
                                }));
                            });
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        }
                    });
                    state = result.state;

                    $.ajax({
                        url: 'http://localhost:3000/getcitylist',
                        type: 'GET',
                        data: {
                            'countrycode': $("#country").val()
                        },
                        success: function (data, textStatus, jqXHR) {
                            var cityDropDown = $('#city');
                            $.each(data, function (index, result) {
                                cityDropDown.append(
                                $('<option/>', {
                                    value: result.cityname,
                                    text: result.cityname
                                }));
                            });
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        }
                    });
                    $("#city").find("option").filter(function (index) {
                        return result.city === $(this).text();
                    }).prop("selected", "selected");
                });
            },
            error: function (jqXHR, textStatus, errorThrown) {
            }
        });
    },
    error: function (jqXHR, textStatus, errorThrown) {
    }
});

请更改您选择的jquery下拉脚本,如下所示。

$("#<%= lstAssignedTo.ClientID %> option").filter(function () {
                 return ($(this).text() == result.d[0]['assigned']);
     }).attr('selected', 'selected');