gpt4 book ai didi

reactjs - 如何处理 Reactjs 中多个输入字段的多个 onChange 函数

转载 作者:行者123 更新时间:2023-12-03 13:58:31 25 4
gpt4 key购买 nike

我有一个表单,其中有 5 个输入文本类型字段和一个输入文件类型字段。我使用了 onChange 函数。

这是表格

<form onSubmit ={this.onSubmit.bind(this)} >
<FormGroup bssize="sm">
<input onChange={this.handleChange} className="form-control" type="text" placeholder="Company Name" name="companyName" ref="companyName"/>
</FormGroup>
<FormGroup bssize="sm">
<input onChange={this.handleChange} className="form-control" type="text" placeholder="Account Name" name="AccountName" ref="AccountName" />
</FormGroup>
<FormGroup bssize="sm">
<input onChange={this.handleChange} className="form-control" type="text" placeholder="Bank Name" name="bankName" ref="bankName"/>
</FormGroup>
<FormGroup bssize="sm">
<input onChange={this.handleChange} className="form-control" type="text" placeholder="Branch" name="branch" ref="branch"/>
</FormGroup>
<FormGroup bssize="sm">
<input onChange={this.handleChange} className="form-control" type="text" placeholder="Account Number" name="accountNo" ref="accountNo"/>
</FormGroup>
<FormGroup>

<span>Bank Book Copy</span> &nbsp;
<input style={{display :'none'}} type="file" onChange={this.fileSelectedHandler} ref={fileInput => this.fileInput = fileInput } />
<Button onClick={()=>this.fileInput.click()} >
<Icon type="upload" /> Click to upload
</Button>
</FormGroup>
<input onClick={this.fileUploadHandler} className="btn btn-primary" bsstyle="success" name="submit" type="submit" value="Submit" />
</form>

现在我的问题是哪个文件最后只填充到我的 API 中。我使用 AXIOS 进行 HTTP 请求。我使用存储连接器来上传图像

下面我提到了我的 onChange 函数

constructor(props){
super(props);
this.state = {
companyName : '',
AccountName : '',
bankName : '' ,
bankBookCpy : '' ,
accountNo : '' ,
branch : '' ,
BankList : '',
selectedFile : null,
}
}

fileSelectedHandler = event =>{
this.setState({
selectedFile: event.target.files[0]
})

}
handleChange = event => {
this.setState({
companyName: event.target.value,
AccountName: event.target.value,
bankName: event.target.value,
accountNo: event.target.value,
branch: event.target.value
});

}
fileUploadHandler = () => {
const fd = new FormData();
fd.append('image',this.state.selectedFile, this.state.selectedFile.name )
axios.post('http://localhost:3000/api/attachmentBanks/bankBookCpy/upload',fd , {
onUploadProgress : ProgressEvent => {
console.log('Upload Progress: ' + Math.round(ProgressEvent.loaded / ProgressEvent.total *100) + '%')
}
})
.then(res => {
this.setState({
BankList: res.data.result.files.image[0].name,
});

});
}

AddMeetup(newMeetup){
axios.request({
method:'post',
url:'http://localhost:3000/api/companyBankDetails',
data : newMeetup
}).then(response => {
this.props.history.push('/');
}).catch(err => console.log(err));
onSubmit(e){
const newMeetup = {
companyName: this.state.companyName,
AccountName : this.state.AccountName,
bankName : this.state.bankName,
branch : this.state.branch,
accountNo : this.state.accountNo,
bankBookCpy : this.state.BankList
}
this.AddMeetup(newMeetup);
e.preventDefault();
}

以上函数仅存储最后的onChange值

最佳答案

你可以这样处理:

  handleChange = event => {
this.setState({ [event.target.name]: event.target.value });
};

关于reactjs - 如何处理 Reactjs 中多个输入字段的多个 onChange 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54046158/

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