【问题标题】:javascript condition for session storage not working as expected会话存储的 javascript 条件未按预期工作
【发布时间】:2016-02-21 10:59:24
【问题描述】:

目前正在处理会话存储,用户必须从第一页中选择一个单选按钮并单击下一步按钮 div 必须在第一页的第二页中显示我已经创建了一个函数,其中包含一组对象第二次使用 set item 存储我试图使用 get item 获取这些值。

我有两种情况

  1. 当用户从第一个无线电组中选择 pg 单选按钮时,如果有任何位置,如 alain / abudhabi 如果用户选择 alain | abudhabi 从位置 从位置然后用户选择 DIP EDU 如果用户单击提交按钮然后在第二页中我需要获得一个复选框并创建应用程序按钮其余的应该被隐藏 - 使用我的代码正在工作
  2. 如果用户从第一个单选组中选择 ug 单选按钮,并且如果用户单击提交按钮,则在第二页中,如果有任何位置,例如 alain / abudhabi,则我需要获取 Just a Pay 按钮 ,但这不是很好帮帮我

这是我的plunker link,就像小提琴一样

这是我从第一页获取项目的代码

function storedata(){
    var storeDate = {};
    storeDate['storedValue1'] = $('input[id=pg]:checked').val();
    storeDate['storedValue2'] = $('#alain').prop('checked'),$('#abudhabi').prop('checked');
    storeDate['storedValue3'] = $('#slt_mjrpg option:selected').val("Dip - Prof. PG Dip in Teaching");

    sessionStorage.setItem('storeDate', JSON.stringify(storeDate));

}

function storedata1(){
    var storeDate1 = {};
    storeDate1['storedValuej'] = $('#slt_mjr option:selected').val("Bachelor of Arts in Persian").text();
    sessionStorage.setItem('storeDate1', JSON.stringify(storeDate1));
    console.log(storeDate1);

}

当我签入会话存储时,我将 storedValue2 设为 false

提供后或在此行

storeDate['storedValue2'] = $('#alain').prop('checked') || $('#abudhabi').prop('checked');

我在 storedValue2 = true 中得到了正确的结果,但我的输出没有按预期工作

在第二页

var otherObj = JSON.parse(sessionStorage.getItem('storeDate'));
var otherObj1 = JSON.parse(sessionStorage.getItem('storeDate1'));
if (otherObj.storedValue1 && otherObj.storedValue2 && otherObj.storedValue3 != "pg") {
    $(".pay_check,.pay_click").show();
    $(".pay_trans").hide();
} else if (otherObj1.storedValuej === "BAP") {
    $('.create_btn,.no_applica').show();
    $('.pay_btn').hide();
} else { * * // I have tried using like this but no use**
    //$('.create_btn,.no_applica,.pay_click').hide();
    $('.pay_btn').show();
}

对这个问题有任何建议

请指导我哪里做错了。提前致谢

【问题讨论】:

  • Here's您的特殊代码请求:)
  • @PDKnight 非常感谢你的震撼:)

标签: javascript jquery html json session-storage


【解决方案1】:

好的,这里有很多问题。多半是因为对函数的误解。

首先,我们可以改变

storeDate['storedValue1'] = $('input[id=pg]:checked').val();
// to
storeDate['storedValue1'] = $('#pg').val();

jQuery 提供的$ 函数根据给定的选择器选择元素。所以$('#pg') 选择id 为pg 的元素。然后val()返回元素的值。

其次,我们需要改变

storeDate['storedValue2'] = $('#alain').prop('checked'),$('#abudhabi').prop('checked');
// to
storeDate['storedValue2'] = $('#alain').prop('checked') || $('#abudhabi').prop('checked');

您只是在这里误解了布尔运算符。 a || b 解析为 true 如果 a b 解析为真。

最后是最坏的罪犯

storeDate['storedValue3'] = $('#slt_mjrpg option:selected').val("Dip - Prof. PG Dip in Teaching");
// to
storeDate['storedValue3'] = $('#slt_mjrpg).val();

val 返回一个元素的值。在选择的情况下,它将返回所选选项的值。提供字符串参数将设置该值。所以我们正在做的只是获取值,我们将在稍后检查该值。

在您的隐藏/显示功能中,我们不需要进行太多更改。我们只是将不适当的val 参数移到这里的相等操作中以获得布尔值。

if (otherObj.storedValue1 && otherObj.storedValue2 && otherObj.storedValue3 === "Dip - Prof. PG Dip in Teaching")

【讨论】:

    【解决方案2】:

    我认为会话存储只使用一页。

    您应该尝试使用 localstorage 代替 sessionStorage。

    试试这个

    function storedata(){
        var storeDate = {};
        storeDate['storedValue1'] = $('input[id=pg]:checked').val();
        storeDate['storedValue2'] = $('input[id=alain]:checked').val();
        storeDate['storedValue3'] = $('#slt_mjrpg option:selected').val("Dip - Prof. PG Dip in Teaching");
    
        localStorage.setItem('storeDate', JSON.stringify(storeDate));
    
    }
    

    var otherObj = JSON.parse(localStorage.getItem('storeDate'));         
            var otherObj1 = JSON.parse(sessionStorage.getItem('storeDate1')); 
            if (otherObj.storedValue1 && otherObj.storedValue2 && otherObj.storedValue3 !="pg") {   
                $(".pay_check,.pay_click").show();  
                $(".pay_trans").hide();
            } else if (otherObj1.storedValuej === "BAP"){   
                $('.create_btn,.no_applica').show();
                $('.pay_btn').hide();               
            } else {
    **// I have tried using like this but no use**
                //$('.create_btn,.no_applica,.pay_click').hide();
                $('.pay_btn').show();       
            }
    

    【讨论】:

    • 抱歉不理解。
    • 我尝试在本地存储中使用本地存储,我们有两个问题 1)一旦页面关闭,如果我打开新页面,本地存储数据仍然可用 2)我已经尝试使用本地存储它没有按预期工作
    猜你喜欢
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2017-12-21
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    相关资源
    最近更新 更多