【问题标题】:how to get user input from antd Input.Password?如何从 antd Input.Password 获取用户输入?
【发布时间】:2019-04-17 16:53:20
【问题描述】:

我正在尝试从 antd input.password 字段获取用户输入。有可能吗?

我没有看到任何关于 antd 文档的信息。不知道有没有可能

我期待用户 input.password 的字符串,因为我会将它们保存到本地存储中

【问题讨论】:

    标签: antd


    【解决方案1】:

    您始终可以使用 onChange 方法,例如 onChange={e => console.log(e.target.value) }

    代码沙盒:https://codesandbox.io/s/5283xn4vo4

    import React from "react";
    import ReactDOM from "react-dom";
    import "antd/dist/antd.css";
    import "./index.css";
    import { Input, Button } from "antd";
    
    class PasswordToLocalStorage extends React.Component {
      state = {
        password: undefined
      };
    
      render = () => {
        return (
          <React.Fragment>
            <Input.Password
              onChange={e => this.setState({ password: e.target.value })}
              placeholder="Enter Password"
            />
            <Button
              onClick={() => {
                if (this.state.password) {
                  localStorage.setItem("password", this.state.password);
                  alert("saved to local storage: " + localStorage.password);
                } else {
                  alert("There is no password to save");
                }
              }}
            >
              Save to localStorage
            </Button>
          </React.Fragment>
        );
      };
    }
    
    ReactDOM.render(
      <PasswordToLocalStorage />,
      document.getElementById("container")
    );
    

    【讨论】:

    • 这将触发重新渲染,似乎没有按预期工作。@Shreyans Shrivastav
    猜你喜欢
    • 2019-10-20
    • 2011-12-13
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    相关资源
    最近更新 更多