如何在 jquery 中将可见性设置回 true

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5495965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:15:34  来源:igfitidea点击:

how can I set visible back to true in jquery

jquery

提问by Jin Yong

I am using the following code to hide a dropdown box:

我正在使用以下代码隐藏下拉框:

  <asp:DropDownList ID="test1" runat="server" DataSourceID="dsTestType" CssClass="maptest1" visible="false"
    DataValueField="test_code" DataTextField="test_desc" AppendDataBoundItems="true" >
    <asp:ListItem></asp:ListItem>
  </asp:DropDownList>   

Somehow I try to show this dropdown by using the following code, but this is just not working for me. Anyone know why?

不知何故,我尝试使用以下代码显示此下拉列表,但这对我不起作用。有谁知道为什么?

$("#test1").show();

采纳答案by OJ.

Using ASP.NET's visible="false"property will set the visibilityattribute where as I think when you call show()in jQuery it modifies the displayattribute of the CSS style.

使用 ASP.NET 的visible="false"属性将设置visibility属性,我认为当您show()在 jQuery 中调用时,它会修改displayCSS 样式的属性。

So doing the latter won't rectify the former.

所以做后者并不能纠正前者。

You need to do this:

你需要这样做:

$("#test1").attr("visibility", "visible");

回答by JohnP

Depends on how you hid it.

取决于你如何隐藏它。

If you used the CSS visibilityvalue then

如果你使用了 CSSvisibility值,那么

$('#test1').css('visibility', 'visible');

If you used CSS `display'

如果你使用 CSS `display'

$('#test1').css('display', 'block'); //or inline or any of the other combos

You might even have made it opacity = 0

你甚至可能让它不透明度= 0

$('#test1').css('opacity', '1');

回答by melaos

Depends, if i remember correctly i think asp.net won't render the html object out when you set visible to false.

视情况而定,如果我没记错的话,我认为当您将可见设置为 false 时,asp.net 不会呈现 html 对象。

If you want to be able to control it from the client side, then you better just include the css value to set it invisible rather than using visible =false.

如果您希望能够从客户端控制它,那么您最好只包含 css 值以将其设置为不可见,而不是使用可见 =false。

回答by Arnoldiusss

Remove the visible="false" attribute and add a CSS class that is not visible by default. Then you should be able to reference the dropdown by the correct id, for example:

去掉visible="false" 属性,添加一个默认不可见的CSS 类。然后您应该能够通过正确的 id 引用下拉列表,例如:

$("#ctl00_cphTest_test1").show();

Above ID you should serach for in the source of the rendered page in your browser.

您应该在浏览器中呈现的页面的源代码中搜索上述 ID。

回答by Damb

How you made it invisible? Try different approach. Use

你是怎么让它隐形的?尝试不同的方法。用

$("#test1").css('display','none');

When you want to hide that element, and then use

当您想隐藏该元素时,然后使用

$("#test1").css('display','block');

When you wnat to show it.

当你想展示它时。

Or just move these styles into a class and add/remove class.

或者只是将这些样式移动到一个类中并添加/删除类。

回答by user1263981

Use style="display:none"in your dropdown list tag and in jquery use the following to display and hide.

使用style="display:none"您的下拉列表标签和jQuery中使用如下的显示和隐藏。

$("#yourdropdownid").css('display', 'inline');

OR

或者

$("#yourdropdownid").css('display', 'none');

回答by Timothy Ruhle

I would be careful with setting the displayof the element to block. Different elements have the standard display as different things. For example setting display to block for a table row in firefox causes the width of the cells to be incorrect.

我会小心display将元素的设置为阻止。不同的元素作为不同的东西有标准的显示。例如,在 Firefox 中将表格行的显示设置为阻止会导致单元格的宽度不正确。

Is the name of the element actually test1. I know that .NET can add extra things onto the start or end. The best way to find out if your selector is working properly is by doing this.

实际上是元素的名称 test1。我知道 .NET 可以在开头或结尾添加额外的东西。确定您的选择器是否正常工作的最佳方法是执行此操作。

alert($('#text1').length);

You might just need to remove the visibility attribute

您可能只需要删除可见性属性

$('#text1').removeAttr('visibility');

回答by Tieson T.

The problem is that since you are using ASP.NET controls with a runatattribute, the ID of the control is not actually "test1". It's "test1" with a long string attached to it.

问题在于,由于您使用的是带有runat属性的ASP.NET 控件,因此控件的 ID 实际上不是“test1”。它是带有长字符串的“test1”。