【问题标题】:How to get selected value on Dropdown component in office-ui-fabric-react?如何在 office-ui-fabric-react 中的下拉组件上获取选定值?
【发布时间】:2020-03-09 07:10:31
【问题描述】:

我正在尝试在我的项目中使用office-ui-fabric-react。但是我在控制选择输入时卡住了。我想在 OnChange 事件中获取所选项目的值。但是event.target 上没有value。这似乎是div,所以它只有textContent。我必须使用ref 吗?但是当我使用ref 时我并不高兴,因为我相信这不是反应方式。

图书馆:https://developer.microsoft.com/en-us/fabric#/controls/web/dropdown

  <Dropdown
      label={'Dropdown'}
      onChange={e => {
          // Not working.
          console.log(e.target.value)
      }}
      options={[
          { text: 'A', key: 'keyA'}, 
          { text: 'B', key: 'keyB'}
      ]}
   />
  1. 有没有不使用ref的解决方案?

  2. 如果我必须使用 ref 我应该怎么做?

【问题讨论】:

    标签: reactjs office-ui-fabric office-ui-fabric-react


    【解决方案1】:

    OMG.. 我应该仔细阅读文档,还有第二个参数。

    <Dropdown
          label={'Dropdown'}
          onChange={(e, selectedOption) => {
              // Now I can access with `selectedOption`
          }}
          options={[
              { text: 'A', key: 'keyA'}, 
              { text: 'B', key: 'keyB'}
          ]}
       />
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 2020-05-17
    • 2020-01-27
    • 1970-01-01
    • 2018-01-12
    相关资源
    最近更新 更多