javascript 在下拉列表中隐藏项目 - C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23659856/
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
Hide item in dropdownlist - C#
提问by weto
I have in dropdownlist some values, (for example 1, 2, 3) and I want to hide one of them (for example 2), then when i click on dropdownlist I want to see only 1 and 3 values...
我在下拉列表中有一些值(例如 1、2、3),我想隐藏其中一个(例如 2),然后当我单击下拉列表时,我只想看到 1 和 3 个值...
I use this code, but it doesnt work...
我使用此代码,但它不起作用...
dropDownList.Items[0].Attributes.Add("style", "display: none");
this solution doesnt work too...
这个解决方案也不起作用......
dropDownList.Items[0].Attributes.Add("style", "visibility: hidden");
but when I for example setting color with this solution, it works...
但是当我例如使用此解决方案设置颜色时,它会起作用......
Do somebody know how to set it in C#?
有人知道如何在 C# 中设置它吗?
Later I want to unhide this value and I want to see all values (1, 2, 3)
后来我想取消隐藏这个值,我想看到所有的值 (1, 2, 3)
Thx.
谢谢。
回答by COLD TOLD
I think you should try removing the value
我认为您应该尝试删除该值
ListItem removeItem=dropDownList.Items.FindByValue("1");
dropDownList.Items.Remove(removeItem);
回答by brothers28
How about do disable it? The code should be something like this:
禁用它怎么样?代码应该是这样的:
dropDownList.Items[0].Enabled = false;
Else you can hide it with jQuery:
否则你可以用 jQuery 隐藏它:
$("#yourDropDownListId option[value='0']").hide();
回答by deostroll
I'll go with COLD TODD's answer. Hiding items of a select box using css is a browser dependent behavior - it may work on some browsers; it may not work on others as expected.
我会同意 COLD TODD 的回答。使用 css 隐藏选择框的项目是一种依赖于浏览器的行为 - 它可能适用于某些浏览器;它可能无法按预期对其他人起作用。
I assume you are talking about a small number of items in a list. You can cache it in the viewstate always. Use a NameValueCollection to store such items.
我假设您正在谈论列表中的少数项目。您可以始终将其缓存在视图状态中。使用 NameValueCollection 来存储此类项目。
I haven't tested in all the browsers. In IE10, you cannot hide items at all. In chrome, you cannot hide the first item at all, no matter what. You can play with the example on jsfiddle:
我还没有在所有浏览器中测试过。在 IE10 中,您根本无法隐藏项目。在 chrome 中,无论如何都无法隐藏第一项。你可以在jsfiddle上玩这个例子:
<select id='myoptions'></select>
<br/>
<br/>Hide:
<br/>
<div class="form-inline">
<select id="ctrls"></select>
<input type="button" class="btn btn-default" value="Go" id="btnhide" />
</div>
onLoadScript:
加载脚本:
var data = {
'foo1': 'bar1',
'foo2': 'bar2',
'foo3': 'bar3',
'foo4': 'bar4',
'foo5': 'bar5'
};
$(function () {
var $sel = $('#myoptions');
var $sel2 = $('#ctrls');
$.each(data, function (idx, val) {
var opt = document.createElement('option');
opt.value = val;
opt.innerHTML = idx;
$sel.append(opt);
$sel2.append($(opt).clone());
});
$('#btnhide').click(function () {
var val = $sel2.val();
$sel.find('option').show();
$sel.find('option[value=' + val + ']').hide();
});
});
回答by user12531839
Assuming you are using an ASP dropdownlist, you just have to add Enabled="False" to whichever one you want to hide.
假设您使用的是 ASP 下拉列表,您只需将 Enabled="False" 添加到您想要隐藏的任何一个。
<asp:DropDownList ID='DDL' runat="server" >
<asp:ListItem>1</asp:ListItem>
<asp:ListItem Enabled="False">2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>