Javascript 使用会话存储为 html 下拉菜单设置和获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33598738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:15:44  来源:igfitidea点击:

set and get value using Session storage for html drop down with jquery

javascriptjqueryhtmllocal-storage

提问by Mahadevan

Working on Session storage where in the first page I have a drop down if user select (.bap_mjr) value from the drop-down and click next button from the first page It will redirect to the second page where I need to show an label.

处理会话存储,如果用户从下拉列表中选择 (.bap_mjr) 值,然后从第一页单击下一步按钮,则在第一页中我有一个下拉列表,它将重定向到我需要显示标签的第二页。

First page HTML

第一页 HTML

  <select class="mslt_Field slt_major slt_mjr ipt_required" id="slt_mjr" name="slt_mjr">
     <option value="">Please Select</optio
     <option value="BA in Arts,Culture&Heritage Ma">BAACHM</option>
     <option id="bap_Mjr" class="bap_Mjr"  value="Bachelor of Arts in Persian">BAP</option>
     <option value="Bachelor of Bus Adminstration">BBA</option>
   </select>

Currently I am trying In First Page this is my jquery code

目前我正在尝试在第一页这是我的 jquery 代码

$("#slt_mjr").change(function (){
                alert($(this).val());
                var dropselvalue = -1;
                if (document.getElementById("bap_Mjr").val())
                {
                    alert("check");
                    dropselvalue = 1;
                }
                if(window.sessionStorage) { 
                   sessionStorage.setItem("dropselvalue", dropselvalue);
               }
            });

Second page

第二页

<div class="pay_click">Welcome to second page</div>

Second page jquery code

第二页jquery代码

var dropselvalue = parseInt(sessionStorage.getItem("dropselvalue"));
    if (dropselvalue != -1) {
        if (dropselvalue === 1) {
            $(".pay_click").show()
        }
        //localStorage.removeItem("CheckedRadioValue");
    }

The session was not stored and I am not able to get the item.

会话未存储,我无法获取该项目。

Where I am doing mistake kindly help me

我做错的地方请帮助我

Thanks in advance

提前致谢

回答by Michael Tranchida

you had an incomplete ending tag, and you were using .val() on javascript, not jquery.

您有一个不完整的结束标记,并且您在 javascript 上使用了 .val(),而不是 jquery。

try this code:

试试这个代码:

html, first page:

html,第一页:

<select id="slt_mjr" name="slt_mjr">
    <option value="">Please Select</option>
    <option value="BA in Arts,Culture&Heritage Ma">BAACHM</option>
    <option id="bap_Mjr" class="bap_Mjr" value="Bachelor of Arts in Persian">BAP</option>
    <option value="Bachelor of Bus Adminstration">BBA</option>
</select>
<a href="deleteme1.html">Link to second page</a>

javascript, first page:

javascript,第一页:

$("#slt_mjr").change(function () {
        //alert($(this).val());
        var dropselvalue = -1;
        if ($("#bap_Mjr").val()) {
            dropselvalue = 1;
        }

        if (window.sessionStorage) {
            sessionStorage.setItem("dropselvalue", dropselvalue);
        }
    });

html, second page:

html,第二页:

<button id="check">Check</button>

javascript, second page

javascript,第二页

$('#check').click(function () {
        var dropselvalue = parseInt(sessionStorage.getItem("dropselvalue"));
        if (dropselvalue != -1) {
            if (dropselvalue === 1) {
                alert();
            }
            //localStorage.removeItem("CheckedRadioValue");
        }
    });