set and get value using Session storage for html d

2019-05-23 12:28发布

问题:

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");
        }
    });