gpt4 book ai didi

javascript - 在 Javascript 开关语句上设置 Cookie

转载 作者:行者123 更新时间:2023-11-30 18:15:05 25 4
gpt4 key购买 nike

我有这个 javascript switch 语句:

function showSideMenu() {
switch (document.forms["culinaryrequest"].culinarytype.selectedIndex) {
case 0:
document.getElementById('case1').style.display="none";
document.getElementById('case2').style.display="none";
document.getElementById('case3').style.display="none";
document.getElementById('case4').style.display="none";
break;
case 1:
document.getElementById('case1').style.display="table-row";
document.getElementById('case2').style.display="none";
document.getElementById('case3').style.display="none";
document.getElementById('case4').style.display="none";
break;
case 2:
document.getElementById('case1').style.display="none";
document.getElementById('case2').style.display="table-row";
document.getElementById('case3').style.display="none";
document.getElementById('case4').style.display="none";
break;
case 3:
document.getElementById('case1').style.display="none";
document.getElementById('case2').style.display="none";
document.getElementById('case3').style.display="table-row";
document.getElementById('case4').style.display="none";
break;
case 4:
document.getElementById('case1').style.display="none";
document.getElementById('case2').style.display="none";
document.getElementById('case3').style.display="none";
document.getElementById('case4').style.display="table";
break;
default:
break;
}
}

当用户从下拉菜单中选择一个选项时,它会显示由上述 switch 语句控制的某个元素。问题出在刷新时,或者如果用户收到错误并被带回页面,该元素将再次隐藏。我想设置一个 cookie 来保持元素显示,但我不确定如何将 cookie 添加到 switch 语句。我能举个例子吗?谢谢!

最佳答案

您可以使用框架来完成这项工作。

<html>
<head>
<title></title>
<style type="text/css">
div.case{
display: none;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js"></script>
<script type="text/javascript">
(function($){
$(document).ready(function(){
// Define cookie key
var cookiekey = "mycookiekey";

// Get cases
var cases = $('div.case').hide();

// Set handler
var handler = $('#culinarytype');

// Show case based on cookie
handler.val($.cookie(cookiekey));

// Define the select function
var selectFromHandler = function(){
selected = handler.val();

// First hide all cases
cases.hide();

// Show only wanted case
if(selected > 0){
$(cases.get(selected - 1)).show();
}

// Set cookie
$.cookie(cookiekey, selected, { expires: 7 });
}

// Based on handler change
handler.change(selectFromHandler);

// Default
selectFromHandler();
});
})(jQuery);
</script>
</head>
<body>
<select id="culinarytype">
<option value="0"></option>
<option value="1">Case 1</option>
<option value="2">Case 2</option>
<option value="3">Case 3</option>
<option value="4">Case 4</option>
</select>

<div class="case">Case 1</div>
<div class="case">Case 2</div>
<div class="case">Case 3</div>
<div class="case">Case 4</div>
</body>
</html>

关于javascript - 在 Javascript 开关语句上设置 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13496069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com