简体   繁体   English

是否可以在一个会话存储中存储多个输入字段值,然后使用会话存储在第二页中进行检索?

[英]Is it possible to store Multiple input field values in one single session storage and retrieve in the second page using Session Storage?

I have a query on session storage how to store three fields value in a single session storage. 我对会话存储有一个查询,如何在单个会话存储中存储三个字段值。 I have three fields two radio button (in different groups) and one drop down in the first page this is how my first will look like. 我有三个字段,两个单选按钮(在不同的组中),并且在第一页中有一个下拉菜单,这就是我的第一个样子。 I was not able to get multiple field. 我无法获得多个领域。

For example if user select pg from first radio group, aa from the second radio group and DIP EDU from the drop down third field where i need to store these values as a single session-storage from first page and i need to get the value from the first page and in the second page based on this value i need to show the div. 例如,如果用户从第一个无线电组中选择pg ,从第二个无线电组中选择aa ,从下拉第三字段中选择DIP EDU ,那么我需要从第一页将这些值存储为单个会话存储,并且我需要从中获取值基于此值的第一页和第二页中,我需要显示div。 I don't no how store multiple field value in one session storage still i tried a bit from OS users idea. 我也不是怎么在一个会话存储中存储多个字段值的,我还是从OS用户的想法中尝试了一下。 But I am not getting the result :( 但我没有得到结果:(

<div class="edu">
   <input type="radio" id="ug" name="edu" class="ug" value="ug" checked="checked">
   <label class="radio-label">Undergraduate</label>
   <input type="radio" id="pg" name="edu" value="pg" class="pg">
   <label class="radio-label">Postgraduate</label>
   <input type="radio" id="cu" name="edu" value="cu" class="cu">
   <label class="radio-label">Continuing Education</label>                          
</div>

And My second div consist of another set of radio button 我的第二个div包含另一组单选按钮

 <div class="rad_cam">
   <input id="ad" class="ad" type="radio" name="cam" value="ad" checked>
   <label class="radio-label">AD</label>
   <input id="AA" class="AA" type="radio" name="cam" value="AA">
   <label class="radio-label">AA</label>
 </div>
<select class="mslt_Field slt_mjrpg ipt_required" id="slt_mjrpg" name="slt_mjrpg">
   <option value="">Please Select</option>
   <option value="Dip - Prof. PG Dip in Teaching ">DIP EDU</option>
   <option value="Master of Science in Finance ">MASF</option>
   <option value="MBA">MBA 2013</option>
</select>

In my second page I have simple div where i need to show a label. 在第二页中,我有一个简单的div,需要在其中显示标签。

 <div>Second page</div>

With my current jQuery code in the first page 在第一页中显示我当前的jQuery代码

function storedata(){
alert("check");
var storeDate = {},key,val;
key = $('#pg,#alain').attr('id');
val = $('#pg,#alain').is(':checked');
storeDate[key] = val;   
sessionStorage.setItem('storeDate', JSON.stringify(storeDate));
console.log(key);
}  

and I am calling the function in next button 我在下一个按钮中调用该函数

this is my second page jquery code 这是我的第二页jQuery代码

var otherObj = JSON.parse(session.getItem('storeDate'));
    if(otherObj.ug && otherObj.alain){
        alert("check");
    }

But I am getting ReferenceError: session is not defined kindly please let me know what i am doing wrong here 但我收到ReferenceError:会话未定义好,请让我知道我在这里做错了

whether my storing json data in jquery is that wrong or ...... :( 我在jquery中存储json数据是错误的还是...... :(

You can convert your input data into an object and then use JSON string to store it under one key. 您可以将输入数据转换为对象,然后使用JSON字符串将其存储在一个键下。 You have to use JSON lib. 您必须使用JSON lib。 https://bestiejs.github.io/json3/#section_3 https://bestiejs.github.io/json3/#section_3
var yourData = {}, key, val; key = $('input.ug').attr('id'); val = $('input.ug').is(':checked'); yourData[key] = val;

You can collect all the key, value pairs using jquery. 您可以使用jquery收集所有键,值对。
eg $('input').map(function(index, ele) { console.log(ele); }); 例如$('input').map(function(index, ele) { console.log(ele); }); You can perform appropriate operation on "ele" for getting required attributes, and store in yourData object. 您可以对“ ele”执行适当的操作以获得所需的属性,并将其存储在yourData对象中。

Finally, you can store the data under one key. 最后,您可以将数据存储在一个键下。
sessionStorage.setItem('yourKey', JSON.stringify(yourData)); And get the JSON string back, and parse it to an object. 并返回JSON字符串,并将其解析为一个对象。 var otherObj = JSON.parse(session.getItem('yourKey')) Now you can perform checks: if(otherObj.ug) { ... } var otherObj = JSON.parse(session.getItem('yourKey'))现在,您可以执行检查: if(otherObj.ug) { ... }

I'm not sure if I understand what you want to do, but it seems you're expecting to get both pg and alain values using $('#pg, #alain') and you treat radio buttons as check boxes, but that doesn't work. 我不确定我是否了解您要做什么,但是似乎您希望使用$('#pg, #alain') alain $('#pg, #alain')同时获得pgalain$('#pg, #alain')并且将单选按钮视为复选框,但是不起作用。

this code should work for saving the values 该代码应该可以保存值

function storedata() {
  var storeDate = {};

  // get the id of the selected radio button
  storeDate['storedValue1'] = $('input[name=edu]:checked').val();
  storeDate['storedValue2'] = $('input[name=cam]:checked').val();

  // save the item
  sessionStorage.setItem('storeDate', JSON.stringify(storeDate));
  console.log(storeDate);
}

And this code can retrieve the saved value 并且此代码可以检索保存的值

var otherObj = JSON.parse(sessionStorage.getItem('storeDate'));
if (otherObj.storedValue1 && otherObj.storedValue2) {
  console.log(otherObj);
}

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

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