在另一页上显示隐藏 div .... 的复选框
Checkbox to show hide div .... on another page
此代码适用于同一页面上的结果。我一直在环顾四周,找不到我想要的东西……我想要的是在第 2 页而不是第 1 页上查看结果……通过使用此代码。也不是通过表单解决方案。
我看到了一些关于使用 cookie 的信息。不确定如何在第 2 页上实施 cookie 以获取第 1 页上的复选框状态。
提前致谢。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Elements Using Checkboxes</title>
<style>
.box{
color: #fff;
padding: 20px;
display: none;
margin-top: 20px;
}
.red{ background: #ff0000; }
.green{ background: #228B22; }
.blue{ background: #0000ff; }
.hello{
display: none;
}
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$('input[type="checkbox"]').click(function(){
var inputValue = $(this).attr("value");
$("." + inputValue).toggle();
});
});
</script>
</head>
<body>
<div>
<label><input type="checkbox" name="colorCheckbox" value="red"> red</label>
<label><input type="checkbox" name="colorCheckbox" value="green"> green</label>
<label><input type="checkbox" name="colorCheckbox" value="blue"> blue</label>
</div>
<div class="red box">You have selected <strong>red checkbox</strong> so i am here</div>
<div class="green box">You have selected <strong>green checkbox</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue checkbox</strong> so i am here</div>
</body>
</html>
您可以使用 localStorage
。试试这个
第 1 页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Elements Using Checkboxes</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
let myColors = JSON.parse(localStorage.getItem('myColors') ?? '[]');
myColors.forEach(color => $(`[name="colorCheckbox"][value="${color}"]`).attr('checked', true));
$('input[type="checkbox"]').click(function(){
if( $(this).is(':checked') ){
myColors = [...myColors, $(this).val()];
}else{
myColors = myColors.filter(color => color !== $(this).val());
}
localStorage.setItem('myColors', JSON.stringify(myColors));
});
});
</script>
</head>
<body>
<div>
<label><input type="checkbox" name="colorCheckbox" value="red"> red</label>
<label><input type="checkbox" name="colorCheckbox" value="green"> green</label>
<label><input type="checkbox" name="colorCheckbox" value="blue"> blue</label>
</div>
</body>
</html>
第 2 页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Elements Using Checkboxes</title>
<style>
.box{
color: #fff;
padding: 20px;
margin-top: 20px;
}
.red{ background: #ff0000; }
.green{ background: #228B22; }
.blue{ background: #0000ff; }
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
let myColors = JSON.parse(localStorage.getItem('myColors') ?? '[]')
.map(color => `<div class="${color} box">You have selected <strong>${color} checkbox</strong> so i am here</div>`)
.join('');
$('#selected-colors').html(myColors)
});
</script>
</head>
<body>
<div id="selected-colors"></div>
</body>
</html>
此代码适用于同一页面上的结果。我一直在环顾四周,找不到我想要的东西……我想要的是在第 2 页而不是第 1 页上查看结果……通过使用此代码。也不是通过表单解决方案。
我看到了一些关于使用 cookie 的信息。不确定如何在第 2 页上实施 cookie 以获取第 1 页上的复选框状态。
提前致谢。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Elements Using Checkboxes</title>
<style>
.box{
color: #fff;
padding: 20px;
display: none;
margin-top: 20px;
}
.red{ background: #ff0000; }
.green{ background: #228B22; }
.blue{ background: #0000ff; }
.hello{
display: none;
}
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$('input[type="checkbox"]').click(function(){
var inputValue = $(this).attr("value");
$("." + inputValue).toggle();
});
});
</script>
</head>
<body>
<div>
<label><input type="checkbox" name="colorCheckbox" value="red"> red</label>
<label><input type="checkbox" name="colorCheckbox" value="green"> green</label>
<label><input type="checkbox" name="colorCheckbox" value="blue"> blue</label>
</div>
<div class="red box">You have selected <strong>red checkbox</strong> so i am here</div>
<div class="green box">You have selected <strong>green checkbox</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue checkbox</strong> so i am here</div>
</body>
</html>
您可以使用 localStorage
。试试这个
第 1 页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Elements Using Checkboxes</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
let myColors = JSON.parse(localStorage.getItem('myColors') ?? '[]');
myColors.forEach(color => $(`[name="colorCheckbox"][value="${color}"]`).attr('checked', true));
$('input[type="checkbox"]').click(function(){
if( $(this).is(':checked') ){
myColors = [...myColors, $(this).val()];
}else{
myColors = myColors.filter(color => color !== $(this).val());
}
localStorage.setItem('myColors', JSON.stringify(myColors));
});
});
</script>
</head>
<body>
<div>
<label><input type="checkbox" name="colorCheckbox" value="red"> red</label>
<label><input type="checkbox" name="colorCheckbox" value="green"> green</label>
<label><input type="checkbox" name="colorCheckbox" value="blue"> blue</label>
</div>
</body>
</html>
第 2 页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Elements Using Checkboxes</title>
<style>
.box{
color: #fff;
padding: 20px;
margin-top: 20px;
}
.red{ background: #ff0000; }
.green{ background: #228B22; }
.blue{ background: #0000ff; }
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
let myColors = JSON.parse(localStorage.getItem('myColors') ?? '[]')
.map(color => `<div class="${color} box">You have selected <strong>${color} checkbox</strong> so i am here</div>`)
.join('');
$('#selected-colors').html(myColors)
});
</script>
</head>
<body>
<div id="selected-colors"></div>
</body>
</html>