[英]How to have select options list open / dropped down by default on page load (plain HTML/CSS or Angular solution)
Interestingly, I do not seam to find a simple solution for a simple requirement of having list of select options open / dropped down on page load.有趣的是,对于在页面加载时打开/下拉选择选项列表的简单要求,我没有找到一个简单的解决方案。
I am working with Angular 8 in case that offers some simple script, but I was assuming HTML/CSS should have this built in with some property - such as: 'isOpen' or 'isDroppedDown' ...我正在使用 Angular 8,以防它提供一些简单的脚本,但我假设 HTML/CSS 应该内置一些属性 - 例如:'isOpen' 或 'isDroppedDown' ...
I see some are resolving it with 'size' or 'multiple', but these change the visual property of the control and I want consistency.我看到有些人用“大小”或“多个”来解决它,但这些改变了控件的视觉属性,我想要一致性。 The control needs to be/look the same (with the little arrow ...)控件需要/看起来相同(带有小箭头......)
Code example: Say we have this list/array in TS/Angular code:代码示例:假设我们在 TS/Angular 代码中有这个列表/数组:
philosophers = ['Aristotle', 'Epicurus', 'Descartes', 'Seneca', 'Plato'];
and we feed it to this template:我们将它提供给这个模板:
<select name="phil_selector" [(ngModel)]="selected_phil">
<option *ngFor="let item of philosophers; index as ix" [value]="ix">
{{item}}
</option>
</select>
// For those who prefer plain HTML:
<select name="phil_selector">
<option value="0"></option>
<option value="1">Aristotle</option>
<option value="2">Epicurus</option>
<option value="3">Descartes</option>
<option value="4">Seneca</option>
<option value="5">Plato</option>
</select>
We do not want any item/philosopher be selected by default, but when the page loads, we want the drop-box be already open, to make it clear to the user, that 1st thing he has to do, is to make his selection from the visible list of options.我们不希望默认选择任何项目/哲学家,但是当页面加载时,我们希望下拉框已经打开,向用户明确表示,他必须做的第一件事就是做出选择从可见的选项列表中。 So he will see this:所以他会看到这个:
And, he just clicks on his choice and goes on ...而且,他只需点击他的选择,然后继续……
To summarize the requirement - we need to have a list of 'select' options open (dropped down) after HTML page has loaded.总结一下需求 - 我们需要在 HTML 页面加载后打开(下拉)一个“选择”选项列表。
If you are willing to use mat-select of Angular Material you can do it like this:如果您愿意使用 Angular Material 的 mat-select,您可以这样做:
import {Component, ViewChild, AfterViewInit} from '@angular/core'; /** * @title Basic select */ @Component({ selector: 'select-overview-example', templateUrl: 'select-overview-example.html', styleUrls: ['select-overview-example.css'], }) export class SelectOverviewExample implements AfterViewInit { @ViewChild('mySelect') mySelect; foods = [ {value: 'steak-0', viewValue: 'Steak'}, {value: 'pizza-1', viewValue: 'Pizza'}, {value: 'tacos-2', viewValue: 'Tacos'} ]; ngAfterViewInit() { this.mySelect.open(); } } /** Copyright 2017 Google Inc. All Rights Reserved. Use of this source code is governed by an MIT-style license that can be found in the LICENSE file at http://angular.io/license */
<mat-form-field> <mat-select #mySelect placeholder="Favorite food"> <mat-option *ngFor="let food of foods" [value]="food.value"> {{ food.viewValue }} </mat-option> </mat-select> </mat-form-field> <br> <button (click)="click()">click</button> <!-- Copyright 2017 Google Inc. All Rights Reserved. Use of this source code is governed by an MIT-style license that can be found in the LICENSE file at http://angular.io/license -->
Or you can try Jquery options like shown here How to open select programmatically或者,您可以尝试此处显示的 Jquery 选项如何以编程方式打开选择
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.