这是我的列表框编辑器模板
@if (ViewData[fieldId + "_list"] is System.Collections.IEnumerable)
{
@Html.ListBox("", new MultiSelectList((System.Collections.IEnumerable)ViewData[fieldId + "_list"], "Value", "Name", Model),
new { @style = "margin:0px; width:225px;", @class = "multi-select", multiple = "multiple", data_placeholder = " " })
}
else
{
@Html.ListBox("", new MultiSelectList(new List<SingleValueHolder<int>>(), Model),
new { @style = "margin:0px; width:225px;", @class = "multi-select", multiple = "multiple", data_placeholder = " " })}
如果我们在这里设置宽度,则设置整个列表框宽度。
答案 0 :(得分:2)
您需要两个具有所需宽度的列表框,另一个保存该值。 然后使用魔术z-索引来隐藏具有大下拉的那个
请参阅 LINK TO JSFIDDLE
<div> <span> <select readonly='readonly' id='sel' name="sometext" style = "width:55px;position: absolute; z-index: -1">
<option>text1</option>
<option>text2</option>
<option>text3</option>
<option >some very long text </option>
</select>
<div style="width:500px;height:25px ;background-color:white; z-index: 10" >
<div id="ddiv" tyle="width:500px;height:25px ;background-color:white; z-index: 15" />
<select id='dummy' style = "width:65px;position: absolute; z-index: 10">
<option style="display:none">text1</option>
<option style="display:none">text2</option>
<option style="display:none">text3</option>
<option style="display:none">some very long text </option>
</select>
</div>
</span>
</div>
并在javascript中
$('#sel').change(function () {
$('#dummy').val($('#sel').val());
});
$('#ddiv').click(function () {
open($('#sel'));
}
);
function open(elem) {
if (document.createEvent) {
var e = document.createEvent("MouseEvents");
e.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
elem[0].dispatchEvent(e);
} else if (element.fireEvent) {
elem[0].fireEvent("onmousedown");
}
}
答案 1 :(得分:-1)
我认为您正在设置整个列表框的html属性,即:@html.Listbox。 所以结果你会得到相同的宽度。