首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Jquery仅显示选中的选项

使用Jquery仅显示选中的选项
EN

Stack Overflow用户
提问于 2020-06-07 22:20:05
回答 2查看 92关注 0票数 0

如何只显示那些选中的选项,而隐藏所有未选中的选项?

我的代码只显示复选框,而不是标签。

代码语言:javascript
复制
$("#checked_show").click(function() {

  $("input").hide();
  $("label").hide();
  $("input:checked").show();
});
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="checkbox" name="locationthemes" id="checkbox-1" value="2" class="custom" />
<label for="checkbox-1">Castle</label>
<input type="checkbox" name="locationthemes" id="checkbox-2" value="3" class="custom" />
<label for="checkbox-2">Barn</label>

<br /><br />

<input type="checkbox" name="location" id="checkbox-3" value="5" class="custom" />
<label for="checkbox-3">Restaurant</label>
<input type="checkbox" name="location" id="checkbox-4" value="8" class="custom" />
<label for="checkbox-4">Bar</label>

<br /><br />

<button id="checked_show">Show only Checked</button>

如何只显示那些选中的选项,而隐藏所有未选中的选项?

我的代码只显示复选框,而不是标签。

EN

回答 2

Stack Overflow用户

发布于 2020-06-07 22:24:52

您应该使用:checked来选择选中的输入,并使用:not()对其求反,还可以使用.next()来隐藏必须遍历所有元素并选择下一个元素的标签,下面是一个有效的代码片段:

代码语言:javascript
复制
$("#checked_show").click(function () {
  if ($("input[type='checkbox']:checked").length) {
    $("input[type='checkbox']:not(:checked)").each(function (idx, el) {
      $(el).next().hide();
      $(el).hide();
    });
  } else {
    console.log('Please select an input!');
  }
  
});
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="checkbox" name="locationthemes" id="checkbox-1" value="2" class="custom" />
<label for="checkbox-1">Castle</label>
<input type="checkbox" name="locationthemes" id="checkbox-2" value="3" class="custom" />
<label for="checkbox-2">Barn</label>

<br /><br />

<input type="checkbox" name="location" id="checkbox-3" value="5" class="custom" />
<label for="checkbox-3">Restaurant</label>
<input type="checkbox" name="location" id="checkbox-4" value="8" class="custom" />
<label for="checkbox-4">Bar</label>

<br /><br />

<button id="checked_show">Show only Checked</button>

票数 0
EN

Stack Overflow用户

发布于 2020-06-07 22:27:07

您可以结合使用input:checked:not()来选择未检查的输入。

代码语言:javascript
复制
$("#checked_show").click(function() {

  $("input:not(:checked)").each((i, el) => {
    $(el).next().hide();
    $(el).hide();
  });

});
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="checkbox" name="locationthemes" id="checkbox-1" value="2" class="custom" />
<label for="checkbox-1">Castle</label>
<input type="checkbox" name="locationthemes" id="checkbox-2" value="3" class="custom" />
<label for="checkbox-2">Barn</label>

<br /><br />

<input type="checkbox" name="location" id="checkbox-3" value="5" class="custom" />
<label for="checkbox-3">Restaurant</label>
<input type="checkbox" name="location" id="checkbox-4" value="8" class="custom" />
<label for="checkbox-4">Bar</label>

<br /><br />

<button id="checked_show">Show only Checked</button>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62246692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档