如何检索会话存储值并将其传递给单选按钮和复选框?

时间:2017-03-09 21:33:31

标签: javascript html json

正如问题所在,您是否可以从会话存储或本地存储中获取值到html上的单选按钮,以及复选框的相同内容?

我的代码:



var customername = {"firstname" : getCookie("firstname"), "lastname" : getCookie("lastname")};

var curcustomer1 = {"firstname" : getCookie("firstname"), "lastname" : getCookie("lastname")};

var lastvist = {"date" : dateall} // only display the date and time

    var myJSON = JSON.stringify(customername);
    var myJSON1 = JSON.stringify(lastvist); // has the date when the user last has visited
    
    var myJSON2 = JSON.stringify(curcustomer1);
    
    var myJSON3full = JSON.stringify(custinfo);
    sessionStorage.setItem("custinfo", myJSON3full);
    var objectfull = sessionStorage.getItem("custinfo");
    objfull = JSON.parse(objectfull);

    var object = sessionStorage.getItem("customername");
    obj = JSON.parse(object);
    
    if(object != myJSON) {
        sessionStorage.setItem("customername", myJSON);
        var object = sessionStorage.getItem("customername");
        obj = JSON.parse(object);

        var curcustomer = customername;
        var myJSONcopy = JSON.stringify(curcustomer);
        var object2 = sessionStorage.setItem("curcustomer", myJSONcopy);
        
        var msg5 = "Welcome ";
        document.getElementById("customer").innerHTML = msg5 + " " + "New Customer";
        document.getElementById("date1").innerHTML = "";
        
        var radiobtn = document.getElementsByName("type");
        if(radiobtn.value != 8) {
            document.elem.type.value="8";
        }
        
        var radiobtn1 = document.getElementsByName("special");
        if(radiobtn1.value != 0) {
            document.elem.special.value="0";
        }
        
        for (var i = 0; i < extras.length; i++) {
            if (extras[i].checked) {
                extras[i].checked = false;
            }
        }
    }
    if(object == myJSONcopy) {
        radiobtn = document.getElementsByClassName("type").innerHTML = sessionStorage.getItem("type");
        radiobtn1 = document.getElementsByClassName("special").innerHTML = sessionStorage.getItem("special");
        checboxes = document.getElementsByClassName("extras").innerHTML = sessionStorage.getItem("extras");
    }
&#13;
    <td>
      <input type="radio" name="type" value="8" checked>Small $8.00
      <br>
      <input type="radio" name="type" value="10">Medium $10.00
      <br>
      <input type="radio" name="type" value="15">Large $15.00
      <br>
      <input type="radio" name="type" value="18">Extra Large $18.00
      <br>
      <br>
    </td>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:-1)

如果您尝试将项目添加到共享相同className的多个按钮,则必须执行以下操作:      var list = document.getElementsByClassName('type'); var i; for (i = 0; n < list.length; ++i) { list[i].value= sessionStorage.getItem('events') }; 但如果它只是一个按钮,那么我建议你使用getElementById 像这样 document.getElementById('Id').value = sessionStorage("events");