简体   繁体   中英

how to set multiple cookies by using json in javascript

it is my html code

<form id="myform" action="#">
  <input type="text" id="barnd_name"/><br/><br/><br/>
  <input type="text" id="work_name"/><br/><br/><br/>
  <input type="text" id="express"><br/><br/>
  <input type="button" value="Submit" id="submit" onclick="setcookie()"/>
  <input type="button" value="get" id="submit" onclick="getcookie()"/>
</form>

and this is js code

  function setcookie() {
    var customobject = {};
    customobject.brandname = document.getElementById('barnd_name').value;
    customobject.workname = document.getElementById('work_name').value;
    customobject.express = document.getElementById('express').value;
    var json = JSON.stringify(customobject);
    document.cookie = "cookie=" + json;
  }

  function getcookie() {
    var namevaluearray = document.cookie.split("=");
    var customobject = JSON.parse(namevaluearray[1]);
    document.getElementById("barnd_name").value = customobject.brandname;
    document.getElementById("work_name").value = customobject.workname;
    document.getElementById("express").value = customobject.express;
  }

but it does not work when I click the setcookie button. I want to set the cookie when I click on the set cookie button and when I click on the get cookie button I want to show the texts that the customer writes in the text box

please help me to solve this problem.thankyou.

it is my html code

<form id="myform" action="#">
  <input type="text" id="barnd_name"/><br/><br/><br/>
  <input type="text" id="work_name"/><br/><br/><br/>
  <input type="text" id="express"><br/><br/>
  <input type="button" value="Submit" id="submit" onclick="setcookie()"/>
  <input type="button" value="get" id="submit" onclick="getcookie()"/>
</form>

and this is js code

  function setcookie() {
    var customobject = {};
    customobject.brandname = document.getElementById('barnd_name').value;
    customobject.workname = document.getElementById('work_name').value;
    customobject.express = document.getElementById('express').value;
    var json = JSON.stringify(customobject);
    document.cookie = "cookie=" + json;
  }

  function getcookie() {
    var namevaluearray = document.cookie.split("=");
    var customobject = JSON.parse(namevaluearray[1]);
    document.getElementById("barnd_name").value = customobject.brandname;
    document.getElementById("work_name").value = customobject.workname;
    document.getElementById("express").value = customobject.express;
  }

but it does not work when I click the setcookie button. I want to set the cookie when I click on the set cookie button and when I click on the get cookie button I want to show the texts that the customer writes in the text box

please help me to solve this problem.thankyou.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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