简体   繁体   English

如何在页面加载时默认打开/下拉选择选项列表(纯 HTML/CSS 或 Angular 解决方案)

[英]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.

相关问题 检测“选择”中下拉选项列表的滚动? - Detect scroll of dropped-down options list in `select`? 多选下拉控件 - Primeng - Angular 6 - 在页面加载时默认选择所有选项并将它们显示为选定标签 - Multiselect dropdown control - Primeng - Angular 6 - Select all options by default on page load and display them as selected label 角度选择默认选项 - Angular Select Default Options JavaScript在下拉列表时清除选择列表 - JavaScript to clear a select list when it is dropped down JSP下拉列表,默认使用struts在页面加载时选择第一个选项 - JSP drop down list to select first option by default on page load using struts 如何使用 CSS 或 JQuery 调整 HTML 选择选项下拉框的大小 - How to resize HTML Select options Drop Down box using CSS or JQuery 如何在Angular JS的选择列表中选择默认下拉选项 - How to select default drop-down option in select list in Angular JS 如何在Drupal 7的下拉HTML选择列表中删除禁用的选项 - How to delete disabled options in a drop-down HTML select list in Drupal 7 在选择相关下拉选项之前,如何在页面加载时默认隐藏文本字段? - How to have text fields hidden by default on page load until the relevant drop down option has been selected? CSS:我怎么能有一个“分散显示单词”而不是html默认显示上/下? - CSS: How can I have a “scatter display of words” rather than the html default display up/down?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM