2016-03-03 46 views
2

我有一個默認禁用的登錄表單提交按鈕。我想在用戶輸入有效的用戶名/密碼時啓用它。有很多使用AngularJS,JQuery和其他基礎設施的例子,但是我想知道使用Material Design Lite做一個乾淨的方法。我當前的代碼是:如何根據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\[email protected]\$%&\*\-_=+! ~\.,\/\\]*" 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> 
) 
} 

}

回答

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> 
+0

確定。這確實處理了按鈕的啓用/禁用,但我實際上正在考慮用戶正在交互式地將數據輸入到用戶名/密碼字段的情況。說用戶名是好的,但密碼字段需要6個字母數字字符才能生效。 像這樣的東西會攔截字符:http://headwayapp.co/widgets/1xGMJQ。 MDL還公開了可以在事件處理程序中檢查的每個字段的有效性檢查。當我回到這裏時,我會嘗試搭起一個jsfiddle。 –