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

泪湿孤枕 提交于 2019-12-22 18:16:16

问题


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.

First page 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

$("#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

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


回答1:


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

try this code:

html, first page:

<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:

$("#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:

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

javascript, second page

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


来源:https://stackoverflow.com/questions/33598738/set-and-get-value-using-session-storage-for-html-drop-down-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!