【问题标题】:React Semantic UI Input not firing function on backspace pressedReact Semantic UI Input 在按下退格键时未触发功能
【发布时间】:2019-10-29 13:00:33
【问题描述】:

我正在使用语义 UI 构建表单。 如果我的某个按钮未完成,我将禁用提交按钮。

<Button
   disabled={!merchantId || !conduit || !region || !acquirer || !processingTimezone}
   content={`Associate to Domain ${DomainData.domain}`}
   onClick={() => this.submitNewMerchant()}
/>

我正在使用函数更改每个字段的状态

// other inputs
<Form.Input
   name="processingTimezone"
   onKeyDown={() => console.log('backspace')}
   onChange={e => this.inputChange(e)}
   label="processingTimezone" 
   defaultValue={processingTimezone}
/>


inputChange = (event) => {
  const { name, value } = event.target;
  this.setState({ [name]: value });
}

如您所见,我正在尝试使用 onChange 和 onKeyDown 来跟踪用户输入,但是如果用户聚焦输入,全选 (ctrl + a) 并按 Backspace,就会出现问题。 状态未更新为空或 '' 或任何其他值。

我该如何管理?

【问题讨论】:

  • 代码对我来说很好。
  • @nithin 你在哪里测试?你有链接吗?
  • 其他按键的状态是否更新?那个输入?
  • @AtinSingh 是的,他们更新正确。

标签: javascript reactjs


【解决方案1】:

尝试在输入文本中添加一个值:

<Form.Input
      name="processingTimezone"
      onKeyDown={() => console.log('backspace')}
      onChange={e => this.inputChange(e)}
      label="processingTimezone" 
      defaultValue="CST"
      value={this.state.processingTimezone}
    />

如果状态不改变,至少输入不会改变。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    相关资源
    最近更新 更多