【问题标题】:Show-Hide LI basis previous selected dropdown optionsShow-Hide LI 基于之前选择的下拉选项
【发布时间】:2021-10-15 17:16:27
【问题描述】:

我需要根据之前下拉菜单中的选择填充图像复选框选择,如下所示:

<div>Select the Brand:
        <select id="ParentBrand"><option></option><option value="Brand1">Brand1</option><option value="Brand2">Brand2</option></select>
    </div>
    <div>Select the Parameter:
        <select id="Parameter"><option></option><option value="Parameter1">Parameter1</option><option value="Parameter2">Parameter2</option></select>
    </div>
<div id=SubBrand style="display: none;">Select the SubBrand:
        <ul>
            <li id="SubBrand1" value="Brand1-Parameter1" style="display: none;">
              <input type="checkbox"/>
              <label for="SubBrand1"><img src="http://townandcountryremovals.com/wp-content/uploads/2013/10/firefox-logo-200x200.png" /></label>
            </li>
            <li id="SubBrand2" value="Brand1-Parameter2" style="display: none;">
              <input type="checkbox"/>
              <label for="SubBrand2"><img src="http://tech21info.com/admin/wp-content/uploads/2013/03/chrome-logo-200x200.png" /></label>
            </li>
            <li id="SubBrand3" value="Brand2-Parameter1" style="display: none;">
              <input type="checkbox"/>
              <label for="SubBrand3"><img src="http://www.thebusinessofsports.com/wp-content/uploads/2010/10/facebook-icon-200x200.png" /></label>
            </li>
            <li id="SubBrand4" value="Brand2-Parameter2" style="display: none;">
                <input type="checkbox"/>
                <label for="SubBrand4"><img src="http://www.thebusinessofsports.com/wp-content/uploads/2010/10/facebook-icon-200x200.png" /></label>
              </li>
          </ul>
    </div>

我的 jQuery 删除 'style="display: none;"' 进行选择,相关的 'li' 如下:

$("#ParentBrand").on('change', function() {
    var ParentBrandSelected = $('#ParentBrand').val();
    
    $("#Parameter").on('change', function() {
      ParameterSelected = $(this).val()
      $("#SubBrand ul li").each(function(e) {
        jQuery(this)
        .filter(($(this).attr('value')).split("-").includes(ParentBrandSelected) && ($(this).attr('value')).split("-").includes(ParameterSelected))
        .css('display', '')
    })

      $("#SubBrand").show();
    });

});

但是,'.css('display', '')' 不起作用,相关的 'li' 仍然隐藏。我该如何完成这项工作?

在这里找到完整的工作代码:https://jsfiddle.net/mh34nfk1/

【问题讨论】:

    标签: jquery dropdown


    【解决方案1】:

    一般来说,您不应该在另一个更改中包含更改方法。

    为了让它工作,你可以这样做:

    jQuery(document).ready(function($) {
      var ParentBrandSelected
      $("#ParentBrand").on('change', function() {
        ParentBrandSelected = $('#ParentBrand').val();
      });
      $("#Parameter").on('change', function() {
        ParameterSelected = $(this).val()
        $("#SubBrand ul li").hide();
        $("#SubBrand ul li[value='" + ParentBrandSelected + "-" + ParameterSelected + "']").show();
        $("#SubBrand").show();
      });
    });
    

    演示

    jQuery(document).ready(function($) {
      var ParentBrandSelected
      $("#ParentBrand").on('change', function() {
        ParentBrandSelected = $('#ParentBrand').val();
      });
      $("#Parameter").on('change', function() {
        ParameterSelected = $(this).val()
        $("#SubBrand ul li").hide();
        $("#SubBrand ul li[value='" + ParentBrandSelected + "-" + ParameterSelected + "']").show();
        $("#SubBrand").show();
      });
    });
    .hide {
        display: none;
    }
    .InnerBlock {
        display:block;
        width: 100%;
        position: relative;
        min-height: 10px;
        margin-top: 5px;
    }
    ul {
      list-style-type: none;
    }
    
    li {
      display: inline-block;
    }
    
    input[type="checkbox"][id^="myCheckbox"] {
      display: none;
    }
    
    label {
      border: 1px solid #fff;
      padding: 10px;
      display: block;
      position: relative;
      margin: 10px;
      cursor: pointer;
    }
    
    label:before {
      background-color: white;
      color: white;
      content: " ";
      display: block;
      border-radius: 50%;
      border: 1px solid grey;
      position: absolute;
      top: -5px;
      left: -5px;
      width: 25px;
      height: 25px;
      text-align: center;
      line-height: 28px;
      transition-duration: 0.4s;
      transform: scale(0);
    }
    
    label img {
      height: 100px;
      width: 100px;
      transition-duration: 0.2s;
      transform-origin: 50% 50%;
    }
    
    :checked + label {
      border-color: #ddd;
    }
    
    :checked + label:before {
      content: "✓";
      background-color: grey;
      transform: scale(1);
    }
    
    :checked + label img {
      transform: scale(0.9);
      /* box-shadow: 0 0 5px #333; */
      z-index: -1;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div>
      <div>Select the Brand:
        <select id="ParentBrand">
          <option></option>
          <option value="Brand1">Brand1</option>
          <option value="Brand2">Brand2</option>
        </select>
      </div>
      <div>Select the Parameter:
        <select id="Parameter">
          <option></option>
          <option value="Parameter1">Parameter1</option>
          <option value="Parameter2">Parameter2</option>
        </select>
      </div>
      <div id=SubBrand style="display: none;">Select the SubBrand:
        <ul>
          <li id="SubBrand1" value="Brand1-Parameter1" style="display: none;">
            <input type="checkbox" />
            <label for="SubBrand1"><img src="http://townandcountryremovals.com/wp-content/uploads/2013/10/firefox-logo-200x200.png" /></label>
          </li>
          <li id="SubBrand2" value="Brand1-Parameter2" style="display: none;">
            <input type="checkbox" />
            <label for="SubBrand2"><img src="http://tech21info.com/admin/wp-content/uploads/2013/03/chrome-logo-200x200.png" /></label>
          </li>
          <li id="SubBrand3" value="Brand2-Parameter1" style="display: none;">
            <input type="checkbox" />
            <label for="SubBrand3"><img src="http://www.thebusinessofsports.com/wp-content/uploads/2010/10/facebook-icon-200x200.png" /></label>
          </li>
          <li id="SubBrand4" value="Brand2-Parameter2" style="display: none;">
            <input type="checkbox" />
            <label for="SubBrand4"><img src="http://www.thebusinessofsports.com/wp-content/uploads/2010/10/facebook-icon-200x200.png" /></label>
          </li>
        </ul>
      </div>
      <div id="DivResult"></div>
    </div>

    我添加了另一个示例,您可以在其中更改两个选择,如果有任何结果,它将显示结果

    演示 2

    jQuery(document).ready(function($) {
      var ParentBrandSelected
      $("#ParentBrand").on('change', function() {
        ParentBrandSelected = $('#ParentBrand').val();
        $("#Parameter").change();
      });
      $("#Parameter").on('change', function() {
        var ParameterSelected = ParentBrandSelected + "-" + $(this).val()
        $("#SubBrand ul li").hide();
        $("#SubBrand ul li[value='" + ParameterSelected + "']").show();
        var n = $("#SubBrand ul li[value='" + ParameterSelected + "']").length > 0;
        $("#SubBrand").toggle(n);
      });
    });
    .hide {
      display: none;
    }
    
    .InnerBlock {
      display: block;
      width: 100%;
      position: relative;
      min-height: 10px;
      margin-top: 5px;
    }
    
    ul {
      list-style-type: none;
    }
    
    li {
      display: inline-block;
    }
    
    input[type="checkbox"][id^="myCheckbox"] {
      display: none;
    }
    
    label {
      border: 1px solid #fff;
      padding: 10px;
      display: block;
      position: relative;
      margin: 10px;
      cursor: pointer;
    }
    
    label:before {
      background-color: white;
      color: white;
      content: " ";
      display: block;
      border-radius: 50%;
      border: 1px solid grey;
      position: absolute;
      top: -5px;
      left: -5px;
      width: 25px;
      height: 25px;
      text-align: center;
      line-height: 28px;
      transition-duration: 0.4s;
      transform: scale(0);
    }
    
    label img {
      height: 100px;
      width: 100px;
      transition-duration: 0.2s;
      transform-origin: 50% 50%;
    }
    
    :checked+label {
      border-color: #ddd;
    }
    
    :checked+label:before {
      content: "✓";
      background-color: grey;
      transform: scale(1);
    }
    
    :checked+label img {
      transform: scale(0.9);
      /* box-shadow: 0 0 5px #333; */
      z-index: -1;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div>
      <div>Select the Brand:
        <select id="ParentBrand">
          <option></option>
          <option value="Brand1">Brand1</option>
          <option value="Brand2">Brand2</option>
        </select>
      </div>
      <div>Select the Parameter:
        <select id="Parameter">
          <option></option>
          <option value="Parameter1">Parameter1</option>
          <option value="Parameter2">Parameter2</option>
        </select>
      </div>
      <div id=SubBrand style="display: none;">Select the SubBrand:
        <ul>
          <li id="SubBrand1" value="Brand1-Parameter1" style="display: none;">
            <input type="checkbox" />
            <label for="SubBrand1"><img src="http://townandcountryremovals.com/wp-content/uploads/2013/10/firefox-logo-200x200.png" /></label>
          </li>
          <li id="SubBrand2" value="Brand1-Parameter2" style="display: none;">
            <input type="checkbox" />
            <label for="SubBrand2"><img src="http://tech21info.com/admin/wp-content/uploads/2013/03/chrome-logo-200x200.png" /></label>
          </li>
          <li id="SubBrand3" value="Brand2-Parameter1" style="display: none;">
            <input type="checkbox" />
            <label for="SubBrand3"><img src="http://www.thebusinessofsports.com/wp-content/uploads/2010/10/facebook-icon-200x200.png" /></label>
          </li>
          <li id="SubBrand4" value="Brand2-Parameter2" style="display: none;">
            <input type="checkbox" />
            <label for="SubBrand4"><img src="http://www.thebusinessofsports.com/wp-content/uploads/2010/10/facebook-icon-200x200.png" /></label>
          </li>
        </ul>
      </div>
      <div id="DivResult"></div>
    </div>

    【讨论】:

    • 嘿,感谢@Carsten 的快速回复,非常感谢您的意见。它们可以根据需要完美地工作。 :) 另外,很想知道为什么“我们不应该在另一个更改中使用更改方法”?它会减慢代码还是容易出错?再次感谢! :)
    • @MithunUchil 在更改内部进行更改没有错误,但有一些原因。 1这只是不好的做法。 2:1机会取决于另一个。也可以多次调用第一次更改可以绑定多个事件,然后如果其中有任何事件。
    • @MithunUchil 以您的代码 jsfiddle.net/2krqua7n 为例。运行第一个选择 2 次。然后运行第二次选择 1 次。您将在控制台日志中看到“运行”一词出现了 2 次。这意味着您的第二个选择触发了 2 次​​span>
    • 感谢@Carsten Løvbo Andersen。这对于像我这样的新手来说非常有用!! :) 非常感谢!
    • @MithunUchil 完全没问题
    猜你喜欢
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多