Добавление атрибутов к кнопке Ahref по уже существующей ссылке - PullRequest
0 голосов
/ 21 апреля 2020

Я пытаюсь добавить атрибуты к ссылке, взятой из тега ahref html, пожалуйста, кто-нибудь может подсказать мне, что я делаю не так в этом?

ссылка будет взята из тега ahref и lang и валюты из выпадающих списков, а затем окончательной ссылкой будет индекс "link +". php? Lang = "+ lang +" ¤cy = "+ currency;"

$(document).ready(function(){
var saveclass = null;

function onChangeHandler() {
  const lang = document.querySelector('#lang').value;
  const currency = document.querySelector('#currency').value;
  var link=document.querySelector('#theButton')..getAttribute('href');
  var strLink = link+"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);




}); 
<!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="price_billing.js"></script>
</head>

<body>

<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="https://alpha.com" id="theButton">Click</a>

  <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="https://alpha.com" id="theButton">Click</a>
  

</body>

</html>

1 Ответ

0 голосов
/ 22 апреля 2020

Я взял трещину в этом. Похоже, у вас есть дополнительный период [.] Перед getAttribute.

Рабочий код:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var saveclass = null;

function onChangeHandler() {
  const lang = document.querySelector('#lang').value;
  const currency = document.querySelector('#currency').value;
  var link=document.querySelector('#theButton').getAttribute('href');
  var strLink = link+"/index.php?lang="+lang+"&currency="+currency;
  document.querySelector('#theButton').setAttribute('href', strLink);

}

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

}); 
</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="https://alpha.com" id="theButton">Click</a>

Я также вынул дополнительный onChangeHandler (); потому что у вас уже есть слушатели смены, выполняющие функцию.

Хотя это технически работает, я не рекомендую его. Если пользователь продолжает изменять параметры, он продолжает добавлять новый URL. Примеры ниже:

https://alpha.com/index.php?lang=French&currency=USD

https://alpha.com/index.php?lang=French&currency=USD / index. php? Lang = French¤cy = EUR

https://alpha.com/index.php?lang=French&currency=USD / index. php? Lang = French¤cy = EUR / index. php? Lang = Spanish¤cy = EUR

https://alpha.com/index.php?lang=French&currency=USD / индекс. php? LANG = French¤cy = EUR / индекс. php? LANG = Spanish¤cy = EUR / индекс. php? LANG = Spanish¤cy = MXN

https://alpha.com/index.php?lang=French&currency=USD / index. php? Lang = French¤cy = EUR / index. php? Lang = Spanish¤cy = EUR / index. php? Lang = Spanish¤cy = MXN / index. php? Lang = английский¤cy = MXN

https://alpha.com/index.php?lang=French&currency=USD / index. php? Lang = French¤cy = EUR / index. php ? LANG = Spanish¤cy = EUR / индекс. php? LANG = Spanish¤cy = MXN / индекс. php? LANG = English¤cy = MXN / индекс. php? LANG = English¤cy = USD

Было бы лучше изменить URL-адрес после того, как пользователи будут на 100% уверены в своих параметрах, а не при каждом отдельном изменении.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...