简体   繁体   English

刷新后如何保留下拉值

[英]How to retain the drop down values after refresh

I'm writing a program where there are 2 drop downs and two buttons, when I select the drop down 1 and dropdown 2 and hit a start, the button has to be disabled and second has to be enabled, and also the dropdowns are to be disabled. 我正在编写一个程序,其中有两个下拉菜单和两个按钮,当我选择下拉菜单1和下拉菜单2并单击开始时,必须禁用按钮,第二个按钮必须启用,并且下拉菜单还被禁用。 And when I do a refresh, the state has to continue the same, Say I selected both dropdowns and hit a start, the dropdowns would be disabled and also the start would be, but the stop would be enabled, when I refresh the form after refresh, it should be the same. 当我进行刷新时,状态必须继续相同,比如说我选择了两个下拉列表并单击了开始,则在我刷新表单后,将禁用下拉列表,也将禁用开始,但是将启用停止刷新,应该相同。 I'm able to use the localStorage and do it for buttons but unable to know how to do the same for dropdowns. 我能够使用localStorage并将其用于按钮,但无法知道如何对下拉菜单执行相同的操作。 Below is my js. 下面是我的js。

$(document).ready(function() {
    if (typeof (Storage) !== "undefined") {
        var stat = localStorage.getItem("clickStat");
        if (stat == "start") {
            $('#Start').attr("disabled", true);
            $('#Stop').attr("disabled", false);

        } else {
            $('#Start').attr("disabled", false);
            $('#Stop').attr("disabled", true);
        }
    }
    var form = $('#formSec');
    var task = document.getElementById('task');
    var subtask = $('#subtask');

    $('#Start').on("click", function() {
        if (typeof (Storage) !== "undefined") {
            localStorage.setItem("clickStat", "start");
        }
        $.ajax({
            type : "post",
            url : "UpdateStartTime",
            data : form.serialize(),
            success : function() {
                $('#task').attr("disabled", true);
                $('#subtask').attr("disabled", true);
                $('#Start').attr("disabled", true);
                $('#Stop').attr("disabled", false);
            }
        });
        return false;
    });

    $('#Stop').on("click", function() {
        if (typeof (Storage) !== "undefined") {
            localStorage.setItem("clickStat", "stop");
        }
        var form = $('#formSec');
        var task = document.getElementById('task');
        var subtask = $('#subtask');
        $.ajax({
            type : "post",
            url : "UpdateEndTime",
            data : form.serialize(),
            success : function() {
                $('#task').attr("disabled", false);
                $('#subtask').attr("disabled", false);
                $('#Start').attr("disabled", false);
                $('#Stop').attr("disabled", true);
            }
        });
        return false;
    });

});

Here is a fiddle. 这是一个小提琴。 https://jsfiddle.net/fx9azgso/2/ https://jsfiddle.net/fx9azgso/2/

Please let me know how Can I fix this. 请让我知道如何解决此问题。

Thanks 谢谢

Hope this helps, I have updated the code for you, look for comments START and END tags in order to get an idea of the changes I have done. 希望这对您有所帮助,我已经为您更新了代码,寻找了注释START和END标签,以便对我所做的更改有所了解。

Please see the working sample @ https://jsfiddle.net/fx9azgso/24/ 请查看工作示例@ https://jsfiddle.net/fx9azgso/24/

Alternatively see the code below: 或者,请参见下面的代码:

    $(document).ready(function() {
    /*Set the variables with appropriate references [START]*/
    var form = $('#formSec');
    var task = $('#task');
    var subtask = $('#subtask');
    /*Set the variables with appropriate references [END]*/

    if (typeof(Storage) !== "undefined") {

        /*Extract values from localStorage [START]*/
        var stat = localStorage.getItem("clickStat");
        var taskVal = localStorage.getItem("taskVal");
        var subtaskVal = localStorage.getItem("subtaskVal");
        /*Extract values from localStorage [END]*/

        if (stat == "start") {
            $('#Start').attr("disabled", true);
            task.attr("disabled", true);
            subtask.attr("disabled", true);
            $('#Stop').attr("disabled", false);

        } else {
            $('#Start').attr("disabled", false);
            task.attr("disabled", false);
            subtask.attr("disabled", false);
            $('#Stop').attr("disabled", true);
        }
    }

    /*If locaStorage values are assigned property i.e. App has been started at least once, assign values [START]*/
    if (taskVal !== null)
        task.val(taskVal);

    if (subtaskVal !== null)
        subtask.val(subtaskVal);
    /*If locaStorage values are assigned property i.e. App has been started at least once, assign values [END]*/


    $('#Start').on("click", function() {
        if (typeof(Storage) !== "undefined") {
            /*Store values in localStorage [START]*/
            localStorage.setItem("clickStat", "start");
            localStorage.setItem("taskVal", task.val());
            localStorage.setItem("subtaskVal", subtask.val());
            /*Store values in localStorage [END]*/
        }
        $.ajax({
            type: "post",
            url: "UpdateStartTime",
            data: form.serialize(),
            success: function() {
                $('#task').attr("disabled", true);
                $('#subtask').attr("disabled", true);
                $('#Start').attr("disabled", true);
                $('#Stop').attr("disabled", false);
            }
        });
        return false;
    });

    $('#Stop').on("click", function() {
        if (typeof(Storage) !== "undefined") {
            /*Store values in localStorage [START]*/
            localStorage.setItem("clickStat", "stop");
            localStorage.setItem("taskVal", task.val());
            localStorage.setItem("subtaskVal", subtask.val());
            /*Store values in localStorage [END]*/
        }
        $.ajax({
            type: "post",
            url: "UpdateEndTime",
            data: form.serialize(),
            success: function() {
                $('#task').attr("disabled", false);
                $('#subtask').attr("disabled", false);
                $('#Start').attr("disabled", false);
                $('#Stop').attr("disabled", true);
            }
        });
        return false;
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM