React.js:输入为空时禁用按钮

时间:2015-05-12 10:11:45

标签: reactjs react-jsx

我是React.js的新手,对不起,如果这个问题听起来太愚蠢了。

当输入字段为空时,我正在尝试禁用按钮。 React中的最佳方法是什么?

我正在做以下事情:

<input ref="email"/>

<button disabled={!this.refs.email}>Let me in</button>

这是对的吗?

这不仅仅是动态属性的重复,因为我也很好奇将数据从一个元素传输/检查到另一个元素。

6 个答案:

答案 0 :(得分:207)

您需要保持输入的当前值处于状态(或传递其值up to a parent via a callback functionsideways&lt;您的应用程序&#39;这里的状态管理解决方案&gt; 使得它最终作为支柱传递回您的组件中,因此您可以为该按钮派生禁用的道具。

使用状态的示例:

&#13;
&#13;
<meta charset="UTF-8">
<script src="https://fb.me/react-0.13.3.js"></script>
<script src="https://fb.me/JSXTransformer-0.13.3.js"></script>
<div id="app"></div>
<script type="text/jsx;harmony=true">void function() { "use strict";

var App = React.createClass({
  getInitialState() {
    return {email: ''}
  },
  handleChange(e) {
    this.setState({email: e.target.value})
  },
  render() {
    return <div>
      <input name="email" value={this.state.email} onChange={this.handleChange}/>
      <button type="button" disabled={!this.state.email}>Button</button>
    </div>
  }
})

React.render(<App/>, document.getElementById('app'))

}()</script>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

使用常量可以组合多个字段进行验证:

&#13;
&#13;
class LoginFrm extends React.Component {
  constructor() {
    super();
    this.state = {
      email: '',
      password: '',
    };
  }
  
  handleEmailChange = (evt) => {
    this.setState({ email: evt.target.value });
  }
  
  handlePasswordChange = (evt) => {
    this.setState({ password: evt.target.value });
  }
  
  handleSubmit = () => {
    const { email, password } = this.state;
    alert(`Welcome ${email} password: ${password}`);
  }
  
  render() {
    const { email, password } = this.state;
    const enabled =
          email.length > 0 &&
          password.length > 0;
    return (
      <form onSubmit={this.handleSubmit}>
        <input
          type="text"
          placeholder="Email"
          value={this.state.email}
          onChange={this.handleEmailChange}
        />
        
        <input
          type="password"
          placeholder="Password"
          value={this.state.password}
          onChange={this.handlePasswordChange}
        />
        <button disabled={!enabled}>Login</button>
      </form>
    )
  }
}

ReactDOM.render(<LoginFrm />, document.body);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<body>


</body>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

另一种检查方法是内联函数,以便在每个渲染(每个道具和状态改变)上都检查条件

const isDisabled = () => 
  // condition check

这有效:

<button
  type="button"
  disabled={this.isDisabled()}
>
  Let Me In
</button>

但这不起作用:

<button
   type="button"
   disabled={this.isDisabled}
>
  Let Me In
</button>

答案 3 :(得分:0)

<button disabled={false}>button WORKS</button>
<button disabled={true}>button DOES NOT work</button>

现在只需使用 useState 或任何其他条件将 true/false 传递到按钮中,假设您使用的是 React。

答案 4 :(得分:0)

const Example = () => {
  
const [value, setValue] = React.useState("");

function handleChange(e) {
    setValue(e.target.value);
  }

return (


<input ref="email" value={value} onChange={handleChange}/>
<button disabled={!value}>Let me in</button> 

);

}
 
export default Example;

答案 5 :(得分:-1)

这很简单,我们假设您通过扩展包含以下内容的Component构成了状态全类

class DisableButton extends Components 
   {

      constructor()
       {
         super();
         // now set the initial state of button enable and disable to be false
          this.state = {isEnable: false }
       }

  // this function checks the length and make button to be enable by updating the state
     handleButtonEnable(event)
       {
         const value = this.target.value;
         if(value.length > 0 )
        {
          // set the state of isEnable to be true to make the button to be enable
          this.setState({isEnable : true})
        }


       }

      // in render you having button and input 
     render() 
       {
          return (
             <div>
                <input
                   placeholder={"ANY_PLACEHOLDER"}
                   onChange={this.handleChangePassword}

                  />

               <button 
               onClick ={this.someFunction}
               disabled = {this.state.isEnable} 
              /> 

             <div/>
            )

       }

   }