gpt4 book ai didi

javascript - 在reactjs中输入字段为空时禁用按钮

转载 作者:行者123 更新时间:2023-12-03 04:09:08 25 4
gpt4 key购买 nike

我是 ReactJS 新手。我想知道为什么我下面的代码不起作用。除了在文本字段为空时禁用“下一步”按钮之外,一切正常。我的期望是,填写​​完所有文本框后,将启用“下一步”按钮。

class Registration extends React.Component {
constructor (props) {
super (props);
this.state = {
selectedGender: null,
errors: [],
initComplete: false
}

this._handleSubmit = this._handleSubmit.bind(this);
this._handleInputMobileOnChange =
this._handleInputMobileOnChange.bind(this);
this.clearError = this.clearError.bind(this);
}
clearError () {
this.setState({ errors: [] });
}

_handleInputMobileOnChange (e) {
e.preventDefault();
this.clearError();
e.target.value = utils.removeNonNumbers(e.target.value);
//this.setState({value: e.target.value})
}

change(e){
if("" != e.target.value){
this.button.disabled = false;
}
else{
this.button.disabled = true;
}
}
render() {
return (
<div className="container-fluid">
<form onSubmit={this._handleSubmit}>
<div className="form-group has-danger">
<label htmlFor="input-MobileNum">number *</label>
<input ref={(ref) => this.inputMobile = ref} type="tel" className={'form-control ' } id="input-MobileNum" onChange={()=>{ this._handleInputMobileOnChange; this.change.bind(this)}} defaultValue=""/>
</div>
<div className="form-group has-danger">
<label htmlFor="input-Email">Email address *</label>
<input ref={(ref) => this.inputEmail = ref} type="email" className={'form-control '} id="input-Email" defaultValue="" onChange={()=>{ this.clearError; this.change.bind(this)}}/>
</div>
<div className="form-group has-danger">
<label htmlFor="input-Invitation">Invitation code</label>
<input ref={(ref) => this.inputInvitation = ref} type="text" className={'form-control '} id="input-Invitation" defaultValue="" onChange={()=>{ this.clearError; this.change.bind(this)}}/>
</div>
<div className="form-group cta">
//NEXT BUTTON
<button type="submit" className="btn btn-primary" ref={(button) => this.button=button}>Next</button>
</div>
</form>
</div>

)
}

谢谢!!!

我将我的代码更新为此。我逐个测试了它。仅移动设备有效,电子邮件和邀请码由于某种原因不起作用。

class Registration extends React.Component {
constructor (props) {
super (props);
this.state = {
selectedGender: null,
errors: [],
initComplete: false
}

this._handleSubmit = this._handleSubmit.bind(this);
this._handleInputMobileOnChange =
this._handleInputMobileOnChange.bind(this);
this._handleInputEmailOnChange =
this._handleInputEmailOnChange.bind(this);
this._handleInputInvitationOnChange =
this._handleInputInvitationOnChange.bind(this);
this.clearError = this.clearError.bind(this);
}
clearError () {
this.setState({ errors: [] });
}

disable(){
let disable = true;
if (this.state.inputMobile || this.state.inputEmail || this.state.inputInvitation) { //I tried && operator, still only mobile works
disable = false;
}
return disable;
}

_handleInputMobileOnChange (e) {
e.preventDefault();
this.clearError();
e.target.value = utils.removeNonNumbers(e.target.value);
this.setState({inputMobile: e.target.value})
}

_handleInputEmailOnChange(e){
e.preventDefault();
this.clearError();
e.target.value = utils.removeNonNumbers(e.target.value);
this.setState({inputEmail: e.target.value})
}

_handleInputInvitationOnChange(e){
e.preventDefault();
this.clearError();
e.target.value = utils.removeNonNumbers(e.target.value);
this.setState({inputInvitation: e.target.value})
}

change(e){
if("" != e.target.value){
this.button.disabled = false;
}
else{
this.button.disabled = true;
}
}
render() {
return (
<div className="container-fluid">
<form onSubmit={this._handleSubmit}>
<div className="form-group has-danger">
<label htmlFor="input-MobileNum">number *</label>
<input ref={(ref) => this.inputMobile = ref} type="tel" className={'form-control ' } id="input-MobileNum" onChange={this._handleInputMobileOnChange}} defaultValue=""/>
</div>
<div className="form-group has-danger">
<label htmlFor="input-Email">Email address *</label>
<input ref={(ref) => this.inputEmail = ref} type="email" className={'form-control '} id="input-Email" defaultValue="" onChange={this._handleInputEmailOnChange}/>
</div>
<div className="form-group has-danger">
<label htmlFor="input-Invitation">Invitation code</label>
<input ref={(ref) => this.inputInvitation = ref} type="text" className={'form-control '} id="input-Invitation" defaultValue="" onChange={this._handleInputInvitationOnChange}/>
</div>
<div className="form-group cta">
//NEXT BUTTON
<button type="submit" className="btn btn-primary" disabled={this.disable()}>Next</button>
</div>
</form>
</div>

)
}

NVM。我真是太傻了。上面的代码有效! :)))

最佳答案

您应该考虑使用受控输入元素而不是不受控制。这让 React 可以管理输入元素的值,这让事情变得更容易。

首先,将每个输入元素的初始值添加到构造函数中。您可能只需要此处为空字符串 ("")。

然后,为每个输入元素添加一个 change 事件监听器,并为每个元素创建一个函数。然后,每个这样的函数应该检查其他元素的值以及它自己的值,然后启用或禁用该按钮。

<小时/>

演示:

class Registration extends React.Component {
constructor (props) {
super(props);
this.state = {
selectedGender: null,
errors: [],
initComplete: false,
buttonIsDisabled: true,
numberValue: "",
emailValue: "",
codeValue: "",
}
this.changeNumber = this.changeNumber.bind(this);
this.changeEmail = this.changeEmail.bind(this)
this.changeCode = this.changeCode.bind(this);
}

changeNumber = (e) => {
let s = true;
if(this.state.emailValue.length && this.state.codeValue.length && e.target.value.length) {
s = false;
}
let val = e.target.value;
//let val = utils.removeNonNumbers(e.target.value);
this.setState({numberValue: val, buttonIsDisabled: s, errors: []});
}

changeEmail = (e) => {
let s = true;
if(this.state.numberValue.length && this.state.codeValue.length && e.target.value.length) {
s = false;
}
this.setState({emailValue: e.target.value, buttonIsDisabled: s, errors: []});
}

changeCode = (e) => {
let s = true;
if(this.state.numberValue.length && this.state.emailValue.length && e.target.value.length) {
s = false;
}
this.setState({codeValue: e.target.value, buttonIsDisabled: s, errors: []});
}

render() {
return (
<div className="container-fluid">
<form onSubmit={this._handleSubmit}>
<div className="form-group has-danger">
<label htmlFor="input-MobileNum">number *</label>
<input type="tel" className={'form-control ' } id="input-MobileNum" onChange={this.changeNumber} />
</div>
<div className="form-group has-danger">
<label htmlFor="input-Email">Email address *</label>
<input type="email" className={'form-control '} id="input-Email" onChange={this.changeEmail} />
</div>
<div className="form-group has-danger">
<label htmlFor="input-Invitation">Invitation code</label>
<input type="text" className={'form-control '} id="input-Invitation" onChange={this.changeCode} />
</div>
<div className="form-group cta">
<button type="submit" className="btn btn-primary" disabled={this.state.buttonIsDisabled} ref={(button) => this.button=button}>Next</button>
</div>
</form>
</div>

)
}}

ReactDOM.render(<Registration />, document.getElementById("myApp"));
<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>
<div id="myApp"></div>

关于javascript - 在reactjs中输入字段为空时禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44406912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com