javascript - 将 HTML 链接到下拉列表的组合

标签 javascript html user-interface

我编写了一个代码来从两个下拉菜单中获取值,现在我想通过组合下拉菜单导航到不同的 URL。

(function() {
  $(".go-btn").on(
    'click',
    function(e) {
      // within this function you should figure out the url
      // to redirect to, depending on the selected values
      // the next line only shows the text of selected values
      $('#selected').html([$('#select1 option:selected').text(),
        $('#select2 option:selected').text()
      ].join('/'));
    });

}());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<div class="selCont">

  <h2>pick an option</h2>

  <select id="select1">
    <option value="1" selected>West</option>
    <option value="2">East</option>
    <option value="3">North</option>
    <option value="4">South</option>
  </select>

  <select id="select2">
    <option value="1" selected>Winter</option>
    <option value="2">Spring</option>
    <option value="3">Summer</option>
    <option value="4">Fall</option>
  </select>

  <button class="go-btn" type="submit">Go</button>

</div>
<div id="selected"></div>

示例:

west + winter to google.com

east + spring to facebook.com

最佳答案

您可以使用 javascript 开关。 已编辑

首先,我将按钮上的 onClick 事件更改为 onClick。 我使用 Dreamweaver 预览在 Safari 中对其进行了测试。


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<div class="selCont">

  <h2>pick an option</h2>

  <select id="select1">
    <option value="1" selected>West</option>
    <option value="2">East</option>
    <option value="3">North</option>
    <option value="4">South</option>
  </select>

  <select id="select2">
    <option value="1" selected>Winter</option>
    <option value="2">Spring</option>
    <option value="3">Summer</option>
    <option value="4">Fall</option>
  </select>

  <button class="go-btn" onClick="run()" type="submit">Go</button>

然后我发出警报以检查发生了什么。之后删除它。


function run(){
var value1 = $('#select1 option:selected').text();
var value2 = $('#select2 option:selected').text();
var valuecombo = value1 + "/" + value2;

switch (valuecombo) {
        default:
       //in case no matches are found this is what should happen. Could be an alert
//WINTER
    case 'West/Winter':

        alert("West/Winter will push you to google.com");
       window.location.href = 'http://www.google.com';
       break;

    case 'East/Winter':

        alert("East/Winter will push you to google.com");
       window.location.href = 'http://www.google.com';
       break;

    case 'North/Winter':

        alert("North/Winter will push you to google.com");
       window.location.href = 'http://www.google.com';
       break;

    case 'South/Winter':

        alert("South/Winter will push you to google.com");
       window.location.href = 'http://www.google.com';
        break;

//SPRING
    case 'West/Spring':

        alert("West/Spring will push you to google.com");
       window.location.href = 'http://www.google.com';
       break;

    case 'East/Spring':
        alert("East/Spring will push you to facebook.com");
       window.location.href = 'http://www.facebook.com';
       break;

    case 'North/Spring':

        alert("North/Spring will push you to google.com");
       window.location.href = 'http://www.google.com';
       break;

    case 'South/Spring':

        alert("South/Spring will push you to google.com");
       window.location.href = 'http://www.google.com';
        break;

//SUMMER
    case 'West/Summer':

        alert("West/Summer will push you to google.com");
       window.location.href = 'http://www.google.com';
       break;

    case 'East/Summer':

        alert("East/Summer will push you to google.com");
       window.location.href = 'http://www.google.com';
       break;

    case 'North/Summer':

        alert("North/Summer will push you to google.com");
       window.location.href = 'http://www.google.com';
       break;

    case 'South/Summer':

        alert("South/Summer will push you to google.com");
       window.location.href = 'http://www.google.com';
        break;

//FALL
    case 'West/Fall':

        alert("West/Fall will push you to google.com");
       window.location.href = 'http://www.google.com';
       break;

    case 'East/Fall':

        alert("East/Fall will push you to google.com");
       window.location.href = 'http://www.google.com';
       break;

    case 'North/Fall':

        alert("North/Fall will push you to google.com");
       window.location.href = 'http://www.google.com';
       break;

    case 'South/Fall':

        alert("South/Fall will push you to google.com");
       window.location.href = 'http://www.google.com';
        break;

//everything else

    }

};

关于javascript - 将 HTML 链接到下拉列表的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52165641/

相关文章:

javascript - 将 CollectionFS 与 Meteor 一起使用时会发生什么 `publish`

javascript - jQuery:如何通过连接 ID 检索元素?

c# - 是否可以访问图形路径中的点?

javascript - 语义 UI 表单 onSuccess 事件未触发

java - 按下Enter后如何防止StyledText中出现新行

javascript:传递函数名称与 IIFE

javascript - 如何将单选按钮的选择限制为一次

html - SVG 引用外部模式 ID

jquery - 如何使输入类型文本看起来像选择下拉列表?

php - 在 php while 循环中从 MySQL 结果打印新的 <div> 的 id 迭代更改