javascript - 如何使语言下拉选择也留在下一页?
问题描述
我正在使用此代码更改按钮的超链接,现在我希望如果该人选择一种语言,例如法语,那么它将在下一页打开时自动选择。如果我从 page1.php 移动到 page2.php。下一页上的语言应自动选择为法语。谁能指导我如何做到这一点?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="custom.js">
</script>
<select name="" id="lang">
<option value="English">English</option>
<option value="French">French</option>
<option value="Spanish">Spanish</option>
</select>
<select name="" id="currency">
<option value="USD">USD</option>
<option value="EUR">EUR</option>
<option value="MXN">MXN</option>
</select>
<a href="" id="theButton">Click</a>
<a href="test2.php">Click</a>
</body>
</html>
$(document).ready(function(){
var saveclass = null;
function onChangeHandler() {
const lang = document.querySelector('#lang').value;
const currency = document.querySelector('#currency').value;
var strLink = "https://example.com/index.php?lang="+lang+"¤cy="+currency;
document.querySelector('#theButton').setAttribute('href', strLink);
}
onChangeHandler();
document.querySelector('#lang').addEventListener('change', onChangeHandler);
document.querySelector('#currency').addEventListener('change', onChangeHandler);
});
解决方案
最新答案
索引.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="custom.js"></script>
<script type="text/javascript" src="cookie_script.js"></script>
</head>
<body>
<select name="" id="lang">
<option <?php if(@$_COOKIE['lang']=="English"){ echo "selected"; }?> value="English">English</option>
<option <?php if(@$_COOKIE['lang']=="French"){ echo "selected"; }?> value="French">French</option>
<option <?php if(@$_COOKIE['lang']=="Spanish"){ echo "selected"; }?> value="Spanish">Spanish</option>
</select>
<select name="" id="currency">
<option <?php if(@$_COOKIE['currency']=="USD"){ echo "selected"; }?> value="USD">USD</option>
<option <?php if(@$_COOKIE['currency']=="EUR"){ echo "selected"; }?> value="EUR">EUR</option>
<option <?php if(@$_COOKIE['currency']=="MXN"){ echo "selected"; }?> value="MXN">MXN</option>
</select>
<a href="test2.php">Click</a>
</body>
</html>
cookie_script.js
$(document).ready(function(){
$('#lang').on('change', function(){ document.cookie = "lang="+$(this).val()+"; path=/"; });
$('#currency').on('change', function(){ document.cookie = "currency="+$(this).val()+"; path=/"; });
});
始终包括
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="cookie_script.js"></script>
...在每一页上
推荐阅读
- javascript - 带有自定义 Hook 的登录表单 React JS
- r - 如何在R中改变闪亮的曲线
- node.js - 错误 TS2305:模块“readline”没有导出的成员“接口”。- Firebase 功能
- c# - 回发期间应用程序缓存是否刷新其超时?
- c# - System.NullReferenceException:“对象引用未设置为对象的实例。” 问题
- javascript - 通过登录获取 Google Ads 客户 ID
- scala - 在 scala 中混合异步库和同步代码的最佳实践是什么
- ios - Swift 和 Firestore 安全规则、firebase 安全
- python - 使用 Python 编辑 Instagram 个人资料
- python - 我无法在 sklearn 管道中“通过”一步