【问题标题】:How to auto fill value in one drop down with the same value selected in another dropdown?如何使用在另一个下拉列表中选择的相同值自动填充一个下拉列表中的值?
【发布时间】:2021-06-24 00:43:29
【问题描述】:
  • 我有两个下拉菜单,两个下拉菜单中的值相同。

  • 不同组件中的预订类别和服务类别下拉菜单。


当我从预订类别下拉列表中选择任何值时,它应该填写在服务类别下拉列表中,并且还允许通过单击下拉列表来更改值。 如果在 Booking Class 下拉列表中未选择任何值,则服务类别下拉列表可以保持原样。


Booking class

Class of service

从 Booking class 下拉菜单中选择 First class 后,当我进入 Class of service 下拉菜单时,它应该自动填写为 First class。在点击下拉列表时,如果需要更改,列表应该是可见的。


预订舱位

<div>
      <label class="col-sm-2">{{'Booking Class' | translate}}</label>
                      <div class="col-sm-3">
                            <ng-select placeholder="Any" [virtualScroll]="true" formControlName="serviceClass">
                              <ng-option *ngFor="let item of bookingClassData[value]="item.classOfServiceName">  {{item.classOfServiceName}}</ng-option>
                            </ng-select>
                      </div>
 </div>

服务等级

<div class="form-group col-md-3">
            <label>{{'Class Of Service' | translate}}</label>
               <ng-select placeholder="Select Class Of Service" [virtualScroll]="true"    formControlName="classOfService">
                  <ng-option *ngFor="let data of classOfServiceData" [value]="data.classOfServiceName">
                    {{data.classOfServiceName}}</ng-option>
                </ng-select>
    </div>

我有单独的 component.html 文件和各自的 component.ts 文件。


【问题讨论】:

    标签: javascript html angular-material frontend angular-ngselect


    【解决方案1】:

    这是一个 onchange 事件监听器来演示这个想法。因此,要查看它的实际效果,您需要更改选择。我通过字典和循环来实现目标。

    //plastic cups can have coffee or tea
    //glass cups can have Iced Coffee, Iced Tea, or Soda
    //and we don't need to include anything here for no cup selected
    cups = {"plastic": ["Coffee", "Tea"], "glass": ["Iced Coffee", "Iced Tea", "Soda"]}
    
    //get the selection box for cups
    cup = document.getElementById("Cup");
    //add an event listener
    cup.addEventListener("change", function() {
    
      //get the selection box for drinks
      drink = document.getElementById("Drink")
      //set its innerHTML to be nothing
      drink.innerHTML = "";
      //if the value from the cups selection box is not nocup (no cup selected)
      if (cup.value != "nocup")
      {
        //loop over the different drinks
        for(var i = 0; i < cups[cup.value].length; i++)
        {
          //and add them to the drink options drop down
          drink.innerHTML += "<option>" + cups[cup.value][i] + "</option>";
        }
      }
      //otherwise, if the cups selection box is "nocup" (no cup is actually selected)
      else
      {
        //set the drinks selection box back to "No cup selected"
        drink.innerHTML = "<option>No cup selected</option>"
      }
    
    });
    <select id="Cup">
      <option value="nocup">
        Select one
      </option>
      <option value="plastic">
        Plastic
      </option>
      <option value="glass">
        Glass
      </option>
    </select>
    
    <select id="Drink">
      <option>
        No cup selected
      </option>
    </select>

    【讨论】:

    • 这是正确的答案,尽管它使用纯 JS 而不是 ng-select(自定义 Angular 库),虽然它还改变了从选择现有选项到填写选项的事实,但概念是一样的。话虽这么说:您可以使用(change)Booking 添加一个函数,该函数在选择更改时读取,而不是通过jsScript 显式添加侦听器@ClarkeGriffin,此时您使用jQuery 从中选择正确的选项Service。这更有意义吗?
    • 是的,我正在尝试添加(更改)。我为此有单独的 component.ts 文件。如果我在 Booking ts 文件中提供该功能,我们如何将其链接到服务?你能提供基于我的sn-p的代码吗? @fabc
    • @ClarkeG 嗯...坦率地说,我通常会通过为 Booking.ts 和 Service.ts 包含一个桥接 component.ts 文件来解决这个问题,该文件导入它们并且只包含该功能(视为两者都是必需的,并且据我所知,当只有其中一个出现在屏幕上时,它们不太可能被调用)。我相信我误解了你问的问题,你能详细说明一下吗? (试图开始聊天,意识到我只能在自己的答案中做到这一点,对此感到抱歉。或者我忘记了如何正确地做到这一点)
    • 即使我也无法创建聊天。我的疑问是我们在哪里定义函数(更改)? @fabc
    • @ShanerM13 ,你的回应最终是更好的,更多的“角度”ic方法需要创建新文件,OP确认他们不能使用......我会赞成你的回答,但你只能这样做一次
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多