英文:
Display select options side by side
问题
可以更改HTML中<select>
元素的普通显示样式吗?
普通的<select>
显示样式如下:
[选择]
| [选项1]
| [选项2]
| [选项3]
是否可能以这种方式显示选项(无下拉菜单,选项并排显示):
[选项1] [选项2] [选项3]
英文:
I would like to know if this is possible to change the normal display style of a select element in html
The normal <select>
display style is like this :
[Select]
| [option1]
| [option2]
| [option3]
Is it possible to display options like this (no dropdown, options side by side) :
[option1] [option2] [option3]
答案1
得分: 1
没有办法更改默认 HTML 选择标签选项的显示属性。您必须使用 JavaScript 创建自定义选择框,或者使用外部 UI 库如 Bootstrap。
另一种解决方法是设置 multiple
属性。
以下是一个示例:
<select name="select" id="select" multiple size="3">
<option value="1">选项 1</option>
<option value="2">选项 2</option>
<option value="3">选项 3</option>
<option value="4">选项 4</option>
<option value="5">选项 5</option>
<option value="6">选项 6</option>
<option value="7">选项 7</option>
</select>
然后,您可以像这样使用 JavaScript 限制多选:
$("#select").change(function () {
if ($("select option:selected").length > 1) {
this.attr("disabled", "disabled");
} else {
this.removeAttr('disabled');
}
});
英文:
There is no way to change display property of default html select tag options. You have to either create custom select using JS or with external UI libraries like bootstrap.
Another workaround is to set multiple
attribute.
Here is an example:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
select {
text-align: center;
background: yellow;
display: block;
white-space: normal;
}
option {
width: 50px;
margin: 5px;
padding: 10px 5px;
border: 1px solid #000;
border-radius: 5px;
display: inline-block;
}
<!-- language: lang-html -->
<select name="select" id="select" multiple size="3">
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
<option value="4">option 4</option>
<option value="5">option 5</option>
<option value="6">option 6</option>
<option value="7">option 7</option>
</select>
<!-- end snippet -->
And then you can limit your multiple select with JS like so:
$("select").change(function () {
if($("select option:selected").length > 1) {
this.attr("disabled","disabled");
} else {
this.removeAttr('disabled');
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论