【问题标题】:how do you enable/disable submit button based on form contents in Material Design Lite?如何根据 Material Design Lite 中的表单内容启用/禁用提交按钮?
【发布时间】:2016-03-03 17:09:52
【问题描述】:

我有一个默认禁用的登录表单的提交按钮。我想在用户输入有效的用户名/密码时启用它。有很多使用 AngularJS、JQuery 和其他基础设施的示例,但我想知道一种使用 Material Design Lite 的简洁方法。我当前的代码是:

class FrontPage extends Component {
  constructor(props, context) {
    super(props, context);
    this.state = { email: '', password: '' };
  }

  handleEmailChange() {
    this.setState({email: e.target.value})
  }

  handlePasswordChange() {
    this.setState({password: e.target.value})
  }

  render() {
    return(
    <form className="frontpage" onSubmit={this.handleLogin}>
      <div className="content-grid mdl-grid">
        <div className="mdl-cell mdl-textfield mdl-js-textfield">
          <input className="mdl-textfield__input" id="email" type="email" value={this.state.email} onChange={this.handleEmailChange}/>
          <label className="mdl-textfield__label" htmlFor="email">email...</label>
          <span className="mdl-textfield__error">Input is not an email address!</span>
        </div>
        <div className="mdl-cell mdl-textfield mdl-js-textfield">
          <input className="mdl-textfield__input" id="password" type="password" pattern="[A-Z,a-z,0-9\?@\$%&\*\-_=+!  ~\.,\/\\]*" value={this.state.password} onChange={this.handlePasswordChange}/>
          <label className="mdl-textfield__label" htmlFor="password">password...</label>
          <span className="mdl-textfield__error">Input can only contain letters, numbers, and some special characters!</span>
        </div>

        <div classname="mdl-cell">
          <button className="mdl-button mdl-button--raised mdl-button--colored mdl-js-ripple-effect" type="submit" name="login" disabled>Login</button>
        </div>
      </div>
    </form>
  )
}

}

【问题讨论】:

    标签: forms button material-design-lite


    【解决方案1】:
    <button id='loginBtn' className="mdl-button mdl-button--raised mdl-button--colored mdl-js-ripple-effect" type="submit" name="login" disabled>Login</button>
    
    <script>
        function Enable() {
            var loginBtn = document.getElementById('loginBtn');
            loginBtn.removeAttribute("disabled");
            componentHandler.upgradeElement(loginBtn);
        }
    
        function Disable() {
            var loginBtn = document.getElementById('loginBtn');
            loginBtn.setAttribute("disabled","");
            componentHandler.upgradeElement(loginBtn);
        }
    </script>
    

    【讨论】:

    • 好的。这确实处理了按钮的启用/禁用,但我实际上是在考虑用户以交互方式将数据输入用户名/密码字段的情况。假设用户名可以,但密码字段需要 6 个字母数字字符才能生效。像这样的东西会拦截字符:headwayapp.co/widgets/1xGMJQ。 MDL 还公开了可以在事件处理程序中检查的每个字段的有效性检查。当我回到这个问题时,我会尝试建立一个 jsfiddle。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多