JQuery版本与Div搜索效果的冲突

时间:2016-03-30 00:43:20

标签: javascript jquery html css

$(document).ready(function() {
  $(".form-select.pais").change(function() {
    var val = $(this).val();
    if (val != 'Any') {
      $("div.views-row, div.[class^='views-field-field-bank-office-pais-value-']").hide();
      $("div.views-row, div.views-field-field-bank-office-pais-value-" + val).show();
    } else {
      $("div.views-row, div.[class^='views-field-field-bank-office-pais-value-']").show();
    }
  });
});
div {
  background-color: #ddf;
  margin: 2px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<div class="test">
  <p>
    Here is a text test!
  </p>
</div>


<select id="json-one" class="form-select pais">
  <option value="Any">Any</option>
  <option value="USA">USA</option>
  <option value="Chile">Chile</option>
  <option value="Spain">Spain</option>
</select>



<div class="views-row">
  <div class="views-field-field-bank-office-pais-value-USA">
    <span class="field-content">this is div USA</span>
  </div>
  <div class="views-field-field-bank-office-pais-value-Chile">
    <span class="field-content">this is div Chile</span>
  </div>
  <div class="views-field-field-bank-office-pais-value-Spain">
    <span class="field-content">this is div Spain</span>
  </div>
</div>

您好,

我分叉了一个具有滑动效果的小提琴,并调整到我隐藏的链接,并根据选择的下拉框显示。我遇到的问题是隐藏页面上的所有div。例如,具有“test”类的div不应该隐藏但是它正在发生。这不是我想到的预期效果。 (已修复)

感谢。

这是代码:

$(document).ready(function() {
  $('select').change(function() {
    var val = $(this).val();
    if (val != 'Any') {
      $('div:not(div.views-row, div.views-field-field-bank-office-pais-value-' + val + ')').hide();
      $("div.views-row, div.views-field-field-bank-office-pais-value-" + val).show();
    } else {
      $("div.views-row, div").show();
    }
  });
});
div {
  background-color: #ddf;
  margin: 2px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select>
  <option value="Any">Any</option>
  <option value="USA">USA</option>
  <option value="Chile">Chile</option>
  <option value="Spain">Spain</option>
</select>

<div class="views-row">
  <div class="views-field views-field-field-bank-office-pais-value-USA">
    <span class="field-content">this is div USA</span>
  </div>
  <div class="views-field views-field-field-bank-office-pais-value-Chile">
    <span class="field-content">this is div Chile</span>
  </div>
  <div class="views-field views-field-field-bank-office-pais-value-Spain">
    <span class="field-content">this is div Spain</span>
  </div>
</div>

小提琴: http://jsfiddle.net/yjc8jsvw/92/

更新: 我分叉了分叉小提琴: http://jsfiddle.net/u96xfo4x/28/

我现在遇到的问题是,这适用于jquery 1.6,但不适用于带有jquery migrate的2.0+。

我需要帮助更新此脚本。

感谢。

1 个答案:

答案 0 :(得分:0)

尝试将JS的第5行更改为:

$('div.views-row div:not(.div-' + val + ')').hide();

请参阅此处的工作小提琴:http://jsfiddle.net/o56g7804/1/其中包含“测试”文字的div在下拉列表更改后保留。