简体   繁体   English

动态下拉菜单-根据JavaScript选择的选项更改按钮URL

[英]Dynamic Dropdown menu - change button URL depending on option selected with JavaScript

So I have a 'cost calculator' which is very similar (in terms of the code) to this example on JSFiddle . 因此,我有一个“成本计算器”,与代码在JSFiddle上的示例非常相似(就代码而言)。

But I would like to take this one step further. 但我想更进一步。 So currently, when you select an option from each of the dropdown menus, the cost is shown straight away which is great. 因此,当前,当您从每个下拉菜单中选择一个选项时,会立即显示成本,这是非常好的。

I would like to now add a button (or link) beneath that, so that depending on the options selected, not only does the cost update, but the button/link updates automatically depending on what is selected so that the user can then go to a new page if this makes sense? 我现在想在其下添加一个按钮(或链接),以便根据所选的选项,不仅会更新成本,而且按钮/链接也会根据所选内容自动更新,以便用户随后转到一个新页面,如果这有意义吗?

And if the user doesn't click the button/link to go to the new page, but then decides to change an option on the dropdown menu, the link will update again automatically. 而且,如果用户没有单击按钮/链接进入新页面,而是决定更改下拉菜单上的选项,则链接将自动再次更新。

Any help would be much appreciated. 任何帮助将非常感激。

<form name="costcalculator">
  <div class="package-type">
    <select name="packageType" id="packageType" onchange="setMonths(this.value)">
      <option value="gold">Gold</option>
      <option value="silver">Silver</option>
      <option value="bronze">Bronze</option>
    </select>
  </div>

  <div class="months">
    <select name="months" id="months">
      <option value="1">1 month</option>
      <option value="2">2 months</option>
      <option value="3">3 months</option>
      <option value="4">4 months</option>
      <option value="5">5 months</option>
      <option value="6">6 months</option>
      <option value="7">7 months</option>
      <option value="8">8 months</option>
      <option value="9">9 months</option>
      <option value="10">10 months</option>
      <option value="11">11 months</option>
      <option value="12">12 months</option>
    </select>
  </div>

  <button type="button" onclick="calculatePrice()">Calculate</button>
  <div id="price"></div> 
</form>

And the JavaScript: 和JavaScript:

var costs = {
    'gold': {'basePrice': 199.99, 'pricePerMonth' : 99.5, 'maxMonths': 12},
  'silver': {'basePrice': 150, 'pricePerMonth' : 75, 'maxMonths': 12},
  'bronze': {'basePrice': 75, 'pricePerMonth' : 37.5, 'maxMonths': 2}
};

function setMonths(package)
{
    var maxMonths = costs[package].maxMonths;
  document.getElementById("months").innerHTML = ''; // Clear all options
  for (var i = 1; i<=maxMonths; i++){
    var opt = document.createElement('option');
    opt.value = i;
    opt.innerHTML = i +  (i > 1 ? ' months' : ' month');
    document.getElementById('months').appendChild(opt);
    }
}

function calculatePrice()
{
    var package = document.getElementById('packageType').value;
  var months = document.getElementById('months').value;
  var price = costs[package].basePrice + (costs[package].pricePerMonth * (months - 1));
  document.getElementById('price').innerHTML = price;
}

On calculate you can create an a element and give it any custom url using setAttribute method. 计算时,您可以创建一个元素,并使用setAttribute方法为它提供任何自定义网址。 On the next click you can provide a check if the a tag already exists and if it does first remove the old a tag then create a new one and update it with new text and link 在下一次单击上,您可以检查a标签是否已经存在,并且是否确实删除了旧标签,然后创建一个新标签并使用新文本和链接对其进行更新。

 var costs = { 'gold': {'basePrice': 199.99, 'pricePerMonth' : 99.5, 'maxMonths': 12}, 'silver': {'basePrice': 150, 'pricePerMonth' : 75, 'maxMonths': 12}, 'bronze': {'basePrice': 75, 'pricePerMonth' : 37.5, 'maxMonths': 2} }; function setMonths(package) { var maxMonths = costs[package].maxMonths; document.getElementById("months").innerHTML = ''; // Clear all options for (var i = 1; i<=maxMonths; i++){ var opt = document.createElement('option'); opt.value = i; opt.innerHTML = i + (i > 1 ? ' months' : ' month'); document.getElementById('months').appendChild(opt); } } function calculatePrice() { var package = document.getElementById('packageType').value; var months = document.getElementById('months').value; var price = costs[package].basePrice + (costs[package].pricePerMonth * (months - 1)); document.getElementById('price').innerHTML = price; if(document.querySelector('#link')) { document.querySelector('#link').parentNode.removeChild(document.querySelector('#link').parentNode.querySelector('#link')) } var a=document.createElement('a'); a.innerHTML=package a.setAttribute('href','/'+package); a.setAttribute('id','link') document.querySelector('#f').appendChild(a) } 
 <form name="costcalculator" id="f"> <div class="package-type"> <select name="packageType" id="packageType" onchange="setMonths(this.value)"> <option value="gold">Gold</option> <option value="silver">Silver</option> <option value="bronze">Bronze</option> </select> </div> <div class="months"> <select name="months" id="months"> <option value="1">1 month</option> <option value="2">2 months</option> <option value="3">3 months</option> <option value="4">4 months</option> <option value="5">5 months</option> <option value="6">6 months</option> <option value="7">7 months</option> <option value="8">8 months</option> <option value="9">9 months</option> <option value="10">10 months</option> <option value="11">11 months</option> <option value="12">12 months</option> </select> </div> <button type="button" onclick="calculatePrice()">Calculate</button> <div id="price"></div> </form> 

Add a button, which triggers a function to generate a link to your next page. 添加一个按钮,该按钮触发一个函数以生成指向您下一页的链接。

Html: <button type="button" onclick="goToSite()">go to Site</button> html: <button type="button" onclick="goToSite()">go to Site</button>

Javascript 使用Javascript

function goToSite()
{
    var package = document.getElementById('packageType').value;
    var months = document.getElementById('months').value;
    var price = costs[package].basePrice + (costs[package].pricePerMonth * (months - 1));

    window.open('https://your.site.com/' + package + '/' + months + '/' + price, '_self');
}

You can read more about opening a page via Javascript here: w3schools Explanation window.open() 您可以在此处阅读有关通过Javascript打开页面的更多信息: w3schools解释window.open()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Javascript根据下拉菜单中选择的选项更改URL - Using Javascript to change URL based on option selected in dropdown 如何使用javascript在父下拉菜单中根据所选选项更改子下拉菜单中的选项? - How to change options in child dropdown depending of selected option in parent dropdown with javascript? Javascript-自定义网址(取决于所选选项) - Javascript - custom url depending on selected option 下拉选项 - 根据所选选项重定向到自定义 url - Dropdown options - redirect to custom url depending to selected option 根据选择的选项更改按钮 url - Change button url according to selected option Jquery / Javascript如何根据下拉菜单中的select选项更改按钮单击function? - Jquery/Javascript how to change button click function based on select option in dropdown menu? 使用javascript下拉选项菜单 - Dropdown option menu with javascript 更改下拉菜单中的选定选项,从而导致onchange事件; JavaScript的 - Changing the selected option in a dropdown menu and so cause an onchange event; javascript Javascript记住从下拉菜单中选择的选项值 - Javascript remember selected option value from dropdown menu HTML &amp; Javascript - 选择下拉菜单选项时显示文本 - HTML & Javascript - make text appear when dropdown menu option selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM