在旧版本的 Firefox 中回发页面时,未在下拉列表中选择值

Values not getting selected into the dropdown when page is postback in older version of firefox

本文关键字:选择 下拉列表 版本 Firefox      更新时间:2023-09-26

我有一个jQuery函数,当页面使用后退按钮回发时执行。但是当页面在旧版本的 Firefox 中回发时,下拉值不会改变。在旧版本中,它只是在页面回发时显示默认值。下面是jQuery函数:

function loadData() {
    try {
        var URL = "/Home/LoadBackdata/" + new Date().getMilliseconds();
        $.post(URL, null, function (data) {
            debugger;
            if (data != "") {
                var backData = data.split(",");
                if (backData[0] != "") {
                    $('#ConsignorAddressCountryId option').removeAttr('selected');
                    // [1]
                    $("#ConsignorAddressCountryId option[value=" + backData[0] +").prop('selected', true);
                    //ConsigneeAddressCountryId is the id of another class which consist of a dropdown 
                    $('#ConsigneeAddressCountryId option').removeAttr('selected');
                    $("#ConsigneeAddressCountryId option[value=" + backData[3] +").prop('selected', true);
                    $('#drpQuantity option').removeAttr('selected');
                    $('#drpQuantity').prop('selectedIndex', ((parseInt(backData[13]) == 0) ? 0 : (parseInt(backData[13]) - 1)));
                }
            }
        });

[1]是我删除默认选定值的地方 - ConsignorAddressCountryId是由下拉列表组成的类的 id。

我也尝试了一些东西。下面是代码:

function loadData() {
    try {
        var URL = "/Home/LoadBackdata/" + new Date().getMilliseconds();
        $.post(URL, null, function (data) {
            debugger;
            if (data != "") {
                var backData = data.split(",");
                if (backData[0] != "") {
                    $('#ConsignorAddressCountryId option').removeAttr('selected');
                    $("#ConsignorAddressCountryId").find('option:Selected').removeAttr("selected");
                    document.getElementById('ConsignorAddressCountryId').selectedIndex = -1;
                    var ttt = $("#ConsignorAddressCountryId option[value=" + backData[0] + "]").text();
                    $("#dvQuoteFrom >div >a >span >.selectBox-label").first().text("ttt");
                    $("#ConsignorAddressCountryId option[value=" + backData[0] + "]").prop('selected', true);
                    $('#ConsigneeAddressCountryId option').removeAttr('selected');
                    document.getElementById('ConsigneeAddressCountryId').selectedIndex = -1;
                    var Consignee = $("#ConsigneeAddressCountryId option[value=" + backData[0] + "]").text();
                    $("#dvQuoteTo >div >a >span >.selectBox-label").first().text(Consignee);
                    $("#ConsigneeAddressCountryId option[value=" + backData[3] + "]").prop('selected', true);
                    $("#drpQuantity").find('option:Selected').removeAttr("selected");
                    document.getElementById('drpQuantity').selectedIndex = -1;
                    var Quantity = $("#drpQuantity option[value=" + backData[13] + "]").text();
                    $("#divQuantity>a >span >.selectBox-label").first().text(parseInt(10));

                    $('#drpQuantity').prop('selectedIndex', ((parseInt(backData[13]) == 0) ? 0 : (parseInt(backData[13]) - 1)));
                }
            }
        });

但它仍然无法在旧版本的Firefox中工作。

尝试使用重置功能喜欢删除选定的:

$('#yourdropdownid').reset();

要设置选定的值,您可以像以下那样使用 val():

$('#yourdropdownid').val(your value);