简体   繁体   English

使用jQuery隐藏元素不会删除元素的空间

[英]Hiding elements using jQuery is not removing the space of the elements

So I was wondering if anyone has an idea how to fix the following problem: 所以我想知道是否有人知道如何解决以下问题:

I have a drop-down menu with several options. 我有一个带有几个选项的下拉菜单。 Each option changes the page, while the header keeps the same pretty much. 每个选项都会更改页面,而标题保持相同。

All is working fine, but there another dropdown that simply either shows all of the options or hides some, to filter. 一切正常,但有另一个下拉列表,只显示所有选项或隐藏一些,过滤。

The problem is when I hide some, it creates some weird spaces in the first drop-down menu. 问题是当我隐藏一些时,它会在第一个下拉菜单中创建一些奇怪的空间。 I know I can create a different drop-down for each option on the second menu, but it seems like that solution is not optimal, so I was wondering if there's another one. 我知道我可以为第二个菜单上的每个选项创建一个不同的下拉菜单,但似乎该解决方案不是最佳的,所以我想知道是否还有另一个。

Here's a simplified fiddle showing my problem: 这是一个简化的小提示,显示我的问题:

 $('#select6, #select5, #select4, #select3, #select2, #select1').remove(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col"> <select class="form-control" id="matrizsele" onchange="location = this.options[this.selectedIndex].value;"> <option disabled></option> <option id="select1" value="{{ route('layouts.documentos.matriz') }}">COMERC</option> <option id="select2" value="{{ route('layouts.documentos.matrizes.matriz2') }}">COMERC</option> <option id="select3" value="{{ route('layouts.documentos.matrizes.matriz3') }}">COMERC</option> <option id="select4" value="{{ route('layouts.documentos.matrizes.matriz4') }}">COMERC</option> <option id="select5" value="{{ route('layouts.documentos.matrizes.matriz5') }}">COMERC</option> <option id="select6" value="{{ route('layouts.documentos.matrizes.matriz6') }}">COMERC</option> <option disabled></option> <option id="select7" value="{{ route('layouts.documentos.matrizes.matriz7') }}">CONTR</option> <option id="select8" value="{{ route('layouts.documentos.matrizes.matriz8') }}">CONTR</option> <option disabled></option> </select> </div> 

JS fiddle JS小提琴

那些额外的空格不是空格,那些是空的禁用选项。从HTML片段中删除以下内容,它应该可以工作。

<option disabled></option>

You need to remove disabled options from your HTML. 您需要从HTML中删除已禁用的选项。

<div class="col"> 
<select class="form-control" id="matrizsele" onchange="location = this.options[this.selectedIndex].value;">
    <option id="select1" value="{{ route('layouts.documentos.matriz') }}">COMERC</option>
    <option id="select2" value="{{ route('layouts.documentos.matrizes.matriz2') }}">COMERC</option>
    <option id="select3" value="{{ route('layouts.documentos.matrizes.matriz3') }}">COMERC</option>
    <option id="select4" value="{{ route('layouts.documentos.matrizes.matriz4') }}">COMERC</option>
    <option id="select5" value="{{ route('layouts.documentos.matrizes.matriz5') }}">COMERC</option>
    <option id="select6" value="{{ route('layouts.documentos.matrizes.matriz6') }}">COMERC</option>
    <option id="select7" value="{{ route('layouts.documentos.matrizes.matriz7') }}">CONTR</option>  
    <option id="select8" value="{{ route('layouts.documentos.matrizes.matriz8') }}">CONTR</option>
</select>
</div>

JS JS

$('#select6, #select5, #select4, #select3, #select2, #select1').hide();

Disabled option are causing the space when you hide other options. 隐藏其他选项时,禁用选项会占用空间。 Here is an updated fiddle. 这是一个更新的小提琴。

You have to remove this(option) instead of disabled. 您必须删除此(选项)而不是禁用。

$("#matrizsele option[value='X']").each(function() {
  $(this).remove();
});

Note: Here X is your option value 注意:这里X是您的选项值

hiding isn't the issue; 隐藏不是问题; try removing meaningless code which is the following : <option disabled></option> 尝试删除无意义的代码,如下所示: <option disabled></option>

Here's a fixed fiddle : https://jsfiddle.net/nf8m46Lm/19/ 这是一个固定的小提琴: https//jsfiddle.net/nf8m46Lm/19/

The weird "spacing" you talk about is from <option disabled></option> . 你谈到的奇怪的“间距”来自<option disabled></option> It's a blank disabled option, of course it displays a blank entry. 这是一个空白禁用选项,当然它显示一个空白条目。 To understand it better, try changing the code to <option disabled>foobarentry</option> . 要更好地理解它,请尝试将代码更改为<option disabled>foobarentry</option>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM