Javascript Jquery-按值设置所选选项不起作用

Javascript Jquery-按值设置所选选项不起作用,javascript,jquery,html,Javascript,Jquery,Html,我已经尝试过谷歌的所有解决方案。我错过了什么?未选择我想要的选项。 这是我遇到问题的代码部分。我还包括了一些我试过的东西 如果重要的话,这个列表是动态构建的,但是通过值选择它的代码在这个过程之后。 我使用的是Chrome版本80.0.3987.149 //编辑从我的应用程序中添加了此代码 const url='/api/Customers'; //填充下拉列表 $.getJSONurl,函数数据{ $.eachdata,功能键,条目{ dropdown.append$.attr'value',

我已经尝试过谷歌的所有解决方案。我错过了什么?未选择我想要的选项。 这是我遇到问题的代码部分。我还包括了一些我试过的东西

如果重要的话,这个列表是动态构建的,但是通过值选择它的代码在这个过程之后。 我使用的是Chrome版本80.0.3987.149

//编辑从我的应用程序中添加了此代码 const url='/api/Customers'; //填充下拉列表 $.getJSONurl,函数数据{ $.eachdata,功能键,条目{ dropdown.append$.attr'value',entry.CustomerID.textentry.Name+'-'+entry.Email; } }; //结束编辑^^^^^ //var customerID=window.location.hash; var customerID=6a1920b2-f388-4790-a720-75048e1407a7//测试用户5 console.logcustomerID; //$'customer-dropdown option[value='+customerID+']'。prop'selected',true; //$customer下拉列表select.valcustomerID; $customer下拉选项[数据值='+customerID+']。attrselected,selected; 选择客户 测试用户1 测试用户2 测试用户3 测试用户4 测试用户5 测试用户6 测试用户7 测试用户8
只需指定要选择的值

//var customerID=window.location.hash; var customerID=6a1920b2-f388-4790-a720-75048e1407a7//测试用户5 //$'customer-dropdown option[value='+customerID+']'。prop'selected',true; //$customer下拉列表select.valcustomerID; $customer-dropdown.valcustomerID; 选择客户 测试用户1 测试用户2 测试用户3 测试用户4 测试用户5 测试用户6 测试用户7 测试用户8
这最终成为我解决.getJSON async引入的计时问题的解决方案:

我只是在构建下拉列表时选择了它:

$.getJSON(url, function (data) {
    $.each(data, function (key, entry) {
        //console.log(entry.CustomerID + " | " + customerID + " | " + (entry.CustomerID == customerID))
        if (entry.CustomerID == customerID) {
            dropdown.append($('<option></option>').attr({ value: entry.CustomerID, selected: "selected" }).text(entry.Name + ' - ' + entry.Email));
            dropdown.val(customerID);
        } else {
            dropdown.append($('<option></option>').attr('value', entry.CustomerID).text(entry.Name + ' - ' + entry.Email));
        }
    })
});

我在代码中尝试了这一点,它总是选择第一个选项。我看在你的测试中,它确实得到了5分。想知道为什么我的应用程序中没有这个功能。你对所有未使用的代码都进行了注释吗?注意:您应该使用$customer下拉列表,而不是$customer下拉列表选择我编辑并添加的动态零件。我想知道这是不是一个时机问题?不知道.getJSON是否是异步的…谷歌说.getJSON是异步的。那一定是我的问题。谢谢