2014-03-27 48 views
0

是否可以定义选择列表的宽度?如果是这样,该怎么办? 选择标记不具有属性宽度。更改选择选项列表的宽度

<select data-bind="options: names, value: selectedName, optionsText: 'Name', optionsValue: Name, optionsCaption: Select Person"></select> 

回答

1

您可以使用attr绑定,并设置宽度的风格:

视图模型:

function ViewModel() { 
    var self = this; 
    self.selectWidth = ko.observable(); 
    self.selectWidthStyle = ko.computed(function() { 
     return "width:" + self.selectWidth() + "px"; 
    }); 
} 

HTML:

<select data-bind="attr: {style: selectWidthStyle}"></select> 

Demo

+0

如果我想硬编码的大小,那么它会像这样:“attr:{style:{width:'500px'}}”? – Henrik

+0

不,如果你想要的静态宽度,然后只是添加样式到HTML:'' – alexmac

+0

还有一件事,如何在一个CSS文件中做到这一点,所以所有的选择列表可以使用相同的风格?谢谢。 – Henrik

2

使用CSS

<style> 
    select { width: 30%; /*or something like it*/ } 
</style>