Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jQueryAjax中使用回调来设置在下拉列表中选择的文本_Javascript_Ajax_Jquery - Fatal编程技术网

Javascript 在jQueryAjax中使用回调来设置在下拉列表中选择的文本

Javascript 在jQueryAjax中使用回调来设置在下拉列表中选择的文本,javascript,ajax,jquery,Javascript,Ajax,Jquery,我正在填充国家/州和城市下拉列表,但我能够填充下拉列表中的数据,但我想要的是也设置我从数据库请求的选项的选定值,但我无法这样做,我所做的是使用回调函数。可能不用回拨就可以完成。下面是我的代码。 var状态 function func(state) { //alert("callback " + state); $('#state option[value="' + state + '"]').prop('selected', 'selected'); } $.ajax({

我正在填充国家/州和城市下拉列表,但我能够填充下拉列表中的数据,但我想要的是也设置我从数据库请求的选项的选定值,但我无法这样做,我所做的是使用回调函数。可能不用回拨就可以完成。下面是我的代码。 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) {
    }
});
函数func(状态){
//警报(“回调”+状态);
$(“#state option[value=“”+state+“]”)。prop('selected','selected');
}
$.ajax({
网址:'http://localhost:3000/getcountrylist',
键入:“GET”,
成功:函数(数据、文本状态、jqXHR){
var countryDropDown=$(“#country”);
var citizenCountryDropDown=$(“#citizencoutny”);
//警报(“国内名单”);
$.each(数据、函数(索引、结果){
countryDropDown.append(
$('', {
值:result.countrycode,
文本:result.countryname
}));
citizenCountryDropDown.append(
$('', {
值:result.countrycode,
文本:result.countryname
}));
});
$.ajax({
网址:'http://localhost:3000/editempform?userid="分裂,,
键入:“GET”,
成功:函数(数据、文本状态、jqXHR){
$.each(数据、函数(索引、结果){
//$(“#国家/地区选项[value=“PK”]”)。prop('selected','selected');
//$(“选择[name=“country”]选项[value=“PK”]”)
$(“#国家”).find(“option”).filter(函数(索引){
返回result.country==$(this.text();
}).道具(“选定”、“选定”);
$.ajax({
网址:'http://localhost:3000/getstatelist',
键入:“GET”,
数据:{
“countrycode”:$(“#country”).val()
},
成功:函数(数据、文本状态、jqXHR、func){
var stateDropDown=$(“#state”);
$.each(数据、函数(索引、结果){
stateDropDown.append(
$('', {
值:result.state,
文本:result.state
}));
});
},
错误:函数(jqXHR、textStatus、errorshown){
}
});
state=result.state;
$.ajax({
网址:'http://localhost:3000/getcitylist',
键入:“GET”,
数据:{
“countrycode”:$(“#country”).val()
},
成功:函数(数据、文本状态、jqXHR){
var cityDropDown=$(“#city”);
$.each(数据、函数(索引、结果){
cityDropDown.append(
$('', {
值:result.cityname,
文本:result.cityname
}));
});
},
错误:函数(jqXHR、textStatus、errorshown){
}
});
$(“#城市”).find(“选项”).filter(函数(索引){
返回result.city==$(this.text();
}).道具(“选定”、“选定”);
});
},
错误:函数(jqXHR、textStatus、errorshown){
}
});
},
错误:函数(jqXHR、textStatus、errorshown){
}
});

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

$("#<%= lstAssignedTo.ClientID %> option").filter(function () {
                 return ($(this).text() == result.d[0]['assigned']);
     }).attr('selected', 'selected'); 
$(“#选项”).filter(函数(){
返回($(this.text()==result.d[0]['assigned']);
}).attr('selected','selected');

理想情况下,您应该只对服务器脚本执行一个ajax请求,该脚本应该在一次设置select的.val()时返回所有相关数据。更好的是,在创建时设置。好的。是的,这是理想的情况。但在上述情况下如何做到这一点呢?