javascript - 如何在选择框中隐藏选定的选项文本,Angular 4

标签 javascript html css angular dropdown

我有这个种族下拉列表。 我想要的是当我选择明确的种族时它不应该出现在选择框中,并且喜欢的框应该是空的。 我该怎么做?

  <select
        #ethnicity
        [(ngModel)]="patient.Ethnicity"
        [class.text-dimmed]="!patient.Ethnicity"
        name="ethnicity"
        id="ethnicity"
        class="form-control input-underline input-lg ml-0">
          <option [ngValue]="null" disabled> Select </option>
          <option [ngValue]="null">Clear ethnicity</option>
          <option  value="Alaska-Native">Alaska Native</option>
          <option value="American-Indian">American-Indian</option>
          <option value="Asian">Asian</option>
          <option value="African-Americans">Black or African American</option>
          <option value="Hispanic">Hispanic or Latino</option>
          <option value="Jewish-Ashkenazi">Jewish - Ashkenazi</option>
          <option value="Jewish-Other">Jewish - Other</option>
          <option value="Native-Hawaiians">Native Hawaiian or Other Pacific Islander </option>
          <option value="Other">Other </option>
          <option value="Unknown">Unknown </option>
          <option value="White-Caucasian">White or Caucasian</option>
      </select>

就像我选中它时,它是这样的

enter image description here

但我想要的是,

enter image description here

这是我的 demo 清晰的种族应该显示在下拉列表中,但是当我选择它时,它不应该像我共享的屏幕截图那样显示在选择框中

我应该怎么做才能解决这种情况? 谢谢。

最佳答案

您可以像这样处理 select 标记的 change 事件

onChange(value) {
    if(value == 'Clear ethnicity'){
      this.Ethnicity = '';
}

<select
            #ethnicity
            [(ngModel)]="Ethnicity"
            [class.text-dimmed]="!Ethnicity"
            name="ethnicity" (change)="onChange($event.target.value)"
            id="ethnicity"
            class="form-control input-underline input-lg ml-0">

演示:https://stackblitz.com/edit/angular-clear-option-select

关于javascript - 如何在选择框中隐藏选定的选项文本,Angular 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55529929/

相关文章:

javascript - 如何使用 JS 自动填写 Google 表单并随机填写

javascript - Summernote 获取我的 id 文本区域

html - flex 盒子的问题

html - 如何在 HTML/CSS 中勾勒文本轮廓

css - 让div运行到屏幕右侧

javascript - 粘性标题捕捉到顶部到早期

javascript - asp.net mvc - 如何在 javascript 中编写 ajax 请求

javascript - 如何在用 JSON 中的 JS 填充的表中进行可操作的工作?

javascript - appendChild 的表单验证

css - 当元素只出现一次时应用CSS?