繁体   English   中英

我如何使用 sessionStorage 存储价值?

[英]how do i store value using sessionStorage?

这是我的代码。 我想存储 sel1.value 以重定向到子域。 此代码不存储 sel1.value。 我为 cookie 设置了警报,我收到了,但是如何存储它?

function alertCookie(puser) {
  document.cookie = "username=" + puser + ";domain=*.ixxco.coop;";
  sessionStorage.setItem("username", sel1.value);
  alert(puser);
  window.open('https://xxxxxxxx:3500/chat.html', 'Saathi ML BOT', 'width=500,height=650,left=900');
}
.chat-btn img {
  width: 100%;
}

.chat-btn {
  width: 90px;
  position: fixed;
  bottom: 15px;
  right: 15px;
  z-index: 99999;
}

.text1 {
  color: #e49012;
  font-size: 15px;
  line-height: .2;
  position: relative;
  top: -14px;
  font-weight: 600;
  text-shadow: 1px 1px 1px #e7ffca;
}
<label for="sel1" class="header-lbl">Party: </label>
<select id="sel1" class="header-cntrl form-control">
  <option value="NRKT01017" selected>NRKT01017- Limited</option>

</select>

<div class="chat-btn">
  <img src="images/chat-btn.png" alt="image" onclick=alertCookie(sel1.value);>
  <span class="text1">Chat Assistance</span>
</div>

我建议你使用事件监听器

 window.addEventListener("load", function() { document.querySelector(".chat-btn").addEventListener("click", alertCookies); document.getElementById("sel1").addEventListener("change", selectedSel); }) function selectedSel() { console.log(this.value); // or whatever you want when the select changes } function alertCookies() { const puser = document.getElementById("sel1").value; console.log(puser); if (puser) { // uncomment when on your server // document.cookie = "username=" + puser + ";domain=*.ixxco.coop;"; // sessionStorage.setItem("username", puser); // window.open('https://xxxxxxxx:3500/chat.html', 'Saathi ML BOT', 'width=500,height=650,left=900'); } }
 .chat-btn img { width: 100%; } .chat-btn { width: 90px; position: fixed; bottom: 15px; right: 15px; z-index: 99999; } .text1 { color: #e49012; font-size: 15px; line-height: .2; position: relative; top: -14px; font-weight: 600; text-shadow: 1px 1px 1px #e7ffca; }
 <label for="sel1" class="header-lbl">Party: </label> <select id="sel1" class="header-cntrl form-control"> <option value="">Select</option> <option value="NRKT01017">NRKT01017- Limited</option> </select> <div class="chat-btn"> <img src="images/chat-btn.png" alt="Click Me"> <span class="text1">Chat Assistance</span> </div>

暂无
暂无

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

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