根据另一个下拉列表选择JavaScript过滤下拉列表

时间:2019-04-23 07:36:56

标签: javascript html bootstrap-4

我有2个单独的下拉列表和“提交”按钮,可根据选择将访问者带到唯一的URL。

我需要获取每个下拉列表以相互过滤。

例如:当某人从第一个下拉菜单中选择“沃尔沃”时,第二个下拉菜单仅显示“红色”和“白色”,依此类推。

HTML

Sub button_click()

Dim x As Long
x = Range("F100").End(xlUp).Row
Set NewBook = Workbooks.Add
Workbooks("wb").Worksheets("ws").Range("A1:F36").Copy 'works

Workbooks("wb").Worksheets("ws").Range(Cells(1,1), Cells(x, 6)).Copy 'does not work run, time error 1004

NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues) 
End Sub

JavaScript

<form>
  <select class="homepage-dropdown col-md-6 col-9" id="carbrand">
    <option value="volvo">Volvo</option>
    <option value="saab" selected="selected">Saab</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
  </select>
<br>
  <select class="homepage-dropdown col-md-6 col-9" id="carcolor">
    <option value="white">White</option>
    <option value="black" selected="selected">Black</option>
    <option value="red">Red</option>
    <option value="blue">Blue</option>
  </select>

  <br/><br/>

  <input id="btnCallURL" type="button" value="بحث">  
</form>

1 个答案:

答案 0 :(得分:2)

编辑是对重复值的问题进行了排序。 我对您的HTML进行了一些更改

    //new
window.addEventListener( "pageshow", function ( event ) {
          var historyTraversal = event.persisted || ( typeof window.performance != "undefined" && window.performance.navigation.type === 2 );
          if ( historyTraversal ) {
            window.location.reload();
          }
        });
<form>
  <select  onChange="populate('carbrand','carcolor');" class="homepage-dropdown col-md-6 col-9" id="carbrand">
     <option selected>select</option> 
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
  </select>
<br>
  <select class="homepage-dropdown col-md-6 col-9" id="carcolor">
    <option id="select" selected>Select color</option>
  </select>
  <br/><br/>
  <input id="btnCallURL" type="button" value="بحث">  
</form>

而且我已经使用 Populate 方法通过Javascript动态输入值。

<script>
function populate(s1,s2){
            var s = document.getElementById(s1);
            var s_a = document.getElementById(s2);


    var select = document.getElementById(s2).options.length;
    for (var i = select; i >0;i-- ) {
        document.getElementById(s2).options.remove(i);
        console.log(i);
    }
     document.getElementById('select').selected='selected';
  var optionArray=[];
            if(s.value == "volvo"){
                 optionArray = ["red|Red","white|White"];
            } 
      else if(s.value == "saab"){
      optionArray = ["white|White","gray|Gray"];
      }
      else if(s.value == "audi"){
       optionArray = ["black|Black","red|REd"];
      }
      else if(s.value == "opel"){
      optionArray = ["red|REd","black|Black"];
      }
            for(var option in optionArray){
                var pair = optionArray[option].split("|");
                var newOption = document.createElement("option");
                newOption.value = pair[0];
                newOption.innerHTML = pair[1];
                s_a.options.add(newOption);
            }
        }
document.getElementById('btnCallURL').onclick = function() { 
    var carbrandObj = document.getElementById('carbrand');
    var carcolorObj = document.getElementById('carcolor');

    var carbrandValue = carbrandObj.options[carbrandObj.selectedIndex].value;
    var carColorValue = carcolorObj.options[carcolorObj.selectedIndex].value;

    jsCallUrl(carbrandValue, carColorValue);
}

function jsCallUrl(carbrandValue, carColorValue){

    var pageUrl = "";

    switch (carbrandValue){
        case "volvo":
            switch (carColorValue){
            case "white":
                pageUrl = "volvo-white-page-url.php";
                break;
            case "black":
                pageUrl = "volvo-black-page-url.php";
                break;
         case "red":
                pageUrl = "volvo-red-page-url.php";
                break;
         case "blue":
                pageUrl = "volvo-blue-page-url.php";
            break;            
          }
          break;
        case "saab":
            switch (carColorValue){
            case "white":
                pageUrl = "saab-white-page-url.php";
                break;
            case "black":
                pageUrl = "saab-black-page-url.php";
                break;
            default:
                pageUrl = "saab-othercolor-page-url.php";
                break;
          }
          break;
    }



    if (pageUrl != ""){
      location.href = "https://domain/" + pageUrl;
    }
}
    </script>