【问题标题】:How to display name of a choosen item from a list inside an alert window如何从警报窗口内的列表中显示所选项目的名称
【发布时间】:2021-06-20 21:42:28
【问题描述】:

每次用户从列表中选择月份时,我都会尝试显示月份名称。 我设法显示警报,但我想知道如何显示所选或选定月份的名称。 我的意思是,当用户选择一个月时,应显示以下文本作为示例

 month choosen is April

请告诉我如何实现它

app.component.html

<div> Months :
    <select (change) = "changemonths($event)">
    <option *ngFor="let i of months">{{i}}</option>
    </select>
</div>

app.component.ts

months = ["Jan", "Feb", "Mar", "April", "May", "Jun",
        "July", "Aug", "Sept", "Oct", "Nov", "Dec"];
        
 changemonths(event) {
  alert("Changed month from the Dropdown" + event.value);
  console.log(event);
 }

【问题讨论】:

标签: javascript node.js angularjs angular


【解决方案1】:

alert("更改月份" + event.target.value);

【讨论】:

    【解决方案2】:

    使用event.target.value 而不是event.value

    changemonths(event) {
        alert("Changed month from the Dropdown" + event.target.value);
        console.log(event);
    }
    

    演示:https://stackblitz.com/edit/angular-ivy-hzdewq?file=src/app/app.component.ts

    【讨论】:

    • 您能告诉我您是怎么知道 .target.value 的吗?我想知道哪个网页或网站提供了这样的详细信息
    • @user2121 您可以只查看事件对象本身(即简单的console.log),您会看到该值位于target.value。 (另见developer.mozilla.org/en-US/docs/Web/API/Event/target
    • 拜托,我上面贴的代码中的日志语句我不知道它的输出在哪里...你能告诉我如何查看日志语句的输出
    • @user2121 你的意思是console.log
    • 只需打开浏览器控制台 (F12)。你是 JS 新手吗?
    猜你喜欢
    • 2013-09-02
    • 2015-09-25
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 2012-01-29
    • 2018-03-01
    • 1970-01-01
    相关资源
    最近更新 更多