Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.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 在从服务器端加载的后退按钮上设置下拉值_Javascript_Jquery_Ajax_Asp.net Mvc - Fatal编程技术网

Javascript 在从服务器端加载的后退按钮上设置下拉值

Javascript 在从服务器端加载的后退按钮上设置下拉值,javascript,jquery,ajax,asp.net-mvc,Javascript,Jquery,Ajax,Asp.net Mvc,我有一个有4个下拉列表的页面。当我选择第一个下拉列表时,会有一个Ajax调用,它会过滤其他3个下拉列表数据,然后单击submit按钮显示第二个页面。现在,当我单击浏览器的back按钮时,第一个页面中的下拉列表必须保留以前填写的数据。只有第一个下拉列表保留数据,其他3个下拉列表不填充以前选择的数据。为此,我在单击表单提交时设置cookie,如下所示: this.SetCookieForPriceGuide = function () { document.cookie = "Veh

我有一个有4个下拉列表的页面。当我选择第一个下拉列表时,会有一个Ajax调用,它会过滤其他3个下拉列表数据,然后单击submit按钮显示第二个页面。现在,当我单击浏览器的back按钮时,第一个页面中的下拉列表必须保留以前填写的数据。只有第一个下拉列表保留数据,其他3个下拉列表不填充以前选择的数据。为此,我在单击表单提交时设置cookie,如下所示:

this.SetCookieForPriceGuide = function () {
        document.cookie = "VehicleMake=" + $("#VehicleMake").val();
        document.cookie = "VehicleYear=" + $("#VehicleYear").val();
        document.cookie = "VehicleModel=" + $("#VehicleModel").val();
        document.cookie = "VehicleDamage=" + $("#VehicleDamage").val();

    };
function getCookie(cookieValue) {
    var textValue = cookieValue + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(textValue) == 0) {
            return c.substring(textValue.length, c.length);
        }
    }
    return "";
}
在button click事件中设置CookieForPriceGuide,在back按钮上调用load函数,在那里我试图设置下拉列表值,如下所示

this.load = function () {
        var VehicleYear = getCookie("VehicleYear");
        var VehicleMake = getCookie("VehicleMake");
        var VehicleModel = getCookie("VehicleModel");
        var VehicleDamage = getCookie("VehicleDamage");
}
getCookie方法如下:

this.SetCookieForPriceGuide = function () {
        document.cookie = "VehicleMake=" + $("#VehicleMake").val();
        document.cookie = "VehicleYear=" + $("#VehicleYear").val();
        document.cookie = "VehicleModel=" + $("#VehicleModel").val();
        document.cookie = "VehicleDamage=" + $("#VehicleDamage").val();

    };
function getCookie(cookieValue) {
    var textValue = cookieValue + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(textValue) == 0) {
            return c.substring(textValue.length, c.length);
        }
    }
    return "";
}
函数getCookie(cookieValue){ var textValue=cookieValue+“=”; var ca=document.cookie.split(“;”); 对于(变量i=0;i您可以按如下方式设置下拉列表的选定值:

this.load = function () {
    setSelectedValue('ddl_VehicleYearId', getCookie("VehicleYear"), 'defaultValue');
    setSelectedValue('ddl_VehicleMakeId', getCookie("VehicleMake") 'defaultValue');
    setSelectedValue('ddl_VehicleModelId', getCookie("VehicleModel") 'defaultValue');
    setSelectedValue('ddl_VehicleDamageId', getCookie("VehicleDamage") 'defaultValue');
}

function setSelectedValue(dropdownId, cookieValue, defaultValue){
    var element = document.getElementById(dropdownId);
    if(cookieValue) // If cookie has value
        element.value = cookieValue;
    else    // set default value
        element.value = defaultValue;
}

后退按钮什么?应用程序后退按钮或浏览器后退按钮。请更具体地描述您的问题。您的getCookie(cookieValue)方法是否在load method中返回准确值?@Prakash我在问题中已经说过,请仔细阅读。。是的,您刚才写的返回准确值的方法是“在后退按钮上”,而不是具体的后退按钮!所以,您刚刚对设置值有疑问,对吗?