首页 > 解决方案 > 如何使语言下拉选择也留在下一页?

问题描述

我正在使用此代码更改按钮的超链接,现在我希望如果该人选择一种语言,例如法语,那么它将在下一页打开时自动选择。如果我从 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+"&currency="+currency;
  document.querySelector('#theButton').setAttribute('href', strLink);

}

onChangeHandler();

document.querySelector('#lang').addEventListener('change', onChangeHandler);
document.querySelector('#currency').addEventListener('change', onChangeHandler);

}); 

标签: javascriptphp

解决方案


最新答案

索引.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>

...在每一页上


推荐阅读