gpt4 book ai didi

javascript - 使用单个事件处理程序更改多个文本框值

转载 作者:行者123 更新时间:2023-11-30 20:25:02 26 4
gpt4 key购买 nike

home.jsx 文件

import React, { Component } from 'react';
// import 'react-dropdown/style.css';
import { Select, Input, Button } from 'antd';
import App from './App';


const { Option } = Select;
const INITIAL_STATE = {
isShowResult: false,

allData: {
languages: [
'/assets/tech logos/Languages/Javascript/angular js.png',
],

frameWorks: [
'/assets/tech logos/IDE/Eclipse.png',
],

buildSystem: [
'/assets/tech logos/Build servers/apache ant.png',
],

bugTracking: [
'/assets/tech logos/Bugtracking/bugzilla.png',
],

textBox1: ' ',

textBox2: ' ',

textBox3: ' ',

textBox4: ' ',
},
};

export default class Home extends Component {
constructor(props) {
super(props);
this.state = INITIAL_STATE;

this.handleChangeLanguages = this.handleChangeLanguages.bind(this);
this.handleChangeFrameworks = this.handleChangeFrameworks.bind(this);
this.handleChangeBuildSystem = this.handleChangeBuildSystem.bind(this);
this.handleChangeBugTracking = this.handleChangeBugTracking.bind(this);
this.handleChangeTextBox = this.handleChangeTextBox.bind(this);
this.handleChangeTextBox1 = this.handleChangeTextBox1.bind(this);
this.handleChangeTextBox2 = this.handleChangeTextBox2.bind(this);
this.handleChangeTextBox3 = this.handleChangeTextBox3.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChangeLanguages(languages) {
this.setState({ languages });
}

handleChangeFrameworks(frameWorks) {
this.setState({ frameWorks });
}

handleChangeBuildSystem(buildSystem) {
this.setState({ buildSystem });
}

handleChangeBugTracking(bugTracking) {
this.setState({ bugTracking });
}

handleChangeTextBox(e1) {
this.setState(prevState => ({
allData: {
...prevState.allData,
textBox1: this.allData,
},
}));
console.log(e1.target.value);
}

// 1st example

// this.setState({
// e1: this.allData.text.value
// },()=> {
// console.log('Output: ' + this.state.content)
// })

// 2nd example

// this.setState(prevState => ({
// ...prevState,
// dragList: {
// ...prevState.dragList,
// [res]: {
// ...prevState.dragList[res],
// [type]: {
// ...prevState.dragList[res][type],
// ...result
// }
// }
// }
// }))

// 3rd example

// ...this.state,
// textArea: {
// ...this.state.textArea,
// foo: foo,
// },
// }, ()=>console.log(this.state.textArea.foo)); //'some foo'
// }

handleChangeTextBox1(e) {
this.setState({
allData: { ...this.prevState, ...this.e },
...this.textBox2 = e.target.value,
});

console.log(e.target.value);
}

handleChangeTextBox2(e) {
this.setState({ allData: { textBox3: e.target.value } });
console.log(e.target.value);
}

handleChangeTextBox3(e) {
this.setState({ allData: { textBox4: e.target.value } });
console.log(e.target.value);
}

handleSubmit(event) {
// event.preventDefault();
this.setState({ isShowResult: true });
}


render() {
const { isShowResult, allData } = this.state;
const {
textBox1, textBox2, textBox3, textBox4,
} = allData;
return (
<div>
<div>
{!isShowResult &&
<form onSubmit={this.handleSubmit} style={{ padding: 30 }}>

{ /* first dropdown */}

<div>
<b> <h3 > What language you like ? </h3> </b >

</div>
<Select mode="multiple" defaultValue="/assets/tech logos/Languages/Javascript/angular js.png" className="selectdiv" onChange={this.handleChangeLanguages}>
<Option value="/assets/tech logos/Languages/Javascript/angular js.png">
<img src="/assets/tech logos/Languages/Javascript/angular js.png" alt="angular" className="image-size-slider" />
</Option>

<Option value="/assets/tech logos/Languages/JAVA/java-logo.jpg">
<img src="/assets/tech logos/Languages/JAVA/java-logo.jpg" alt="java" className="image-size-slider" />
</Option>

<Option value="/assets/tech logos/Languages/Javascript/react js.png">
<img src="/assets/tech logos/Languages/Javascript/react js.png" alt="reactjs" className="image-size-slider" />
</Option>

<Option value="/assets/tech logos/Languages/Javascript/node js.png">
<img src="/assets/tech logos/Languages/Javascript/node js.png" alt="nodejs" className="image-size-slider" />
</Option>

<Option value="/assets/tech logos/Languages/Microsoft/AspDotNet.png">
<img src="/assets/tech logos/Languages/Microsoft/AspDotNet.png" alt=".net" className="image-size-slider" />
</Option>

<Option value="/assets/tech logos/Languages/Python/Python.png">
<img src="/assets/tech logos/Languages/Python/Python.png" alt="python" className="image-size-slider" />
</Option>

<Option value="/assets/tech logos/Languages/C++.png">
<img src="/assets/tech logos/Languages/C++.png" alt="c++" className="image-size-slider" />
</Option>

</Select>

{ /* second dropdown */}

<div >
<b > <h3 > Which STACK / IDE you like ? </h3></b >
</div>

<Select mode="multiple" defaultValue="/assets/tech logos/IDE/Eclipse.png" className="selectdiv" onChange={this.handleChangeFrameworks}>

<Option value="/assets/tech logos/IDE/Eclipse.png">
<img src="/assets/tech logos/IDE/Eclipse.png" alt="eclipse" className="image-size-slider" />
</Option>

<Option value="/assets/tech logos/IDE/IntelliJ_IDEA_Logo.svg.png">
<img src="/assets/tech logos/IDE/IntelliJ_IDEA_Logo.svg.png" alt="intellij" className="image-size-slider" />
</Option>
<Option value="/assets/tech logos/IDE/visual_studio_purple (1).png">
<img src="/assets/tech logos/IDE/visual_studio_purple (1).png" alt="angular" className="image-size-slider" />
</Option>
</Select>

{/* third dropdown */}

<div>
<b> <h3> Which Build Servers you like ? </h3> </b>
</div>
<Select mode="multiple" defaultValue="/assets/tech logos/Build servers/apache ant.png" style={{ padding: 10 }} onChange={this.handleChangeBuildSystem} >
<Option value="/assets/tech logos/Build servers/apache ant.png">
<img src="/assets/tech logos/Build servers/apache ant.png" alt="apache" className="image-size-slider" />
</Option>
<Option value="/assets/tech logos/Build servers/bamboo-darkbluetext_medium_trans.png">
<img src="/assets/tech logos/Build servers/bamboo-darkbluetext_medium_trans.png" alt="bamboo" className="image-size-slider" />
</Option>
<Option value="/assets/tech logos/Build servers/gitlab.png">
<img src="/assets/tech logos/Build servers/gitlab.png" alt="gitlab" className="image-size-slider" />
</Option>
<Option value="assets/tech logos/Build servers/maven.png">
<img src="/assets/tech logos/Build servers/maven.png" alt="maven" className="image-size-slider" />
</Option>
<Option value="/assets/tech logos/Build servers/vsts-2015.png">
<img src="/assets/tech logos/Build servers/vsts-2015.png" alt="vsts" className="image-size-slider" />
</Option>

</Select>

{/* fourth dropdown */}

<div>
<b> <h3> Which Bug Tracking Software you like ? </h3> </b>
</div>
<Select mode="multiple" defaultValue="/assets/tech logos/Bugtracking/bugzilla.png" style={{ padding: 10 }} onChange={this.handleChangeBugTracking} >
<Option value="/assets/tech logos/Bugtracking/bugzilla.png">
<img src="/assets/tech logos/Bugtracking/bugzilla.png" alt="bugzilla" className="image-size-slider" />
</Option>
<Option value="/assets/tech logos/Bugtracking/jira.png">
<img src="/assets/tech logos/Bugtracking/jira.png" alt="jira" className="image-size-slider" />
</Option>
<Option value="/assets/tech logos/Bugtracking/Micro focus ALM.jpeg">
<img src="/assets/tech logos/Bugtracking/Micro focus ALM.jpeg" alt="focusAlm" className="image-size-slider" />
</Option>
</Select>
<div className="textarea example-input" >
<div>
<b>
<h3>
Show me your interest.
</h3>
</b>
<Input size="large" value={textBox1} onChange={this.handleChangeTextBox} />
</div>
<div>
<b>
<h3>
Show me your interest.
</h3>
</b>
<Input size="large" value={textBox2} onChange={this.handleChangeTextBox1} />
</div>
<div>
<b>
<h3>
Show me your interest.
</h3>
</b>
<Input size="large" value={textBox3} onChange={this.handleChangeTextBox2} />
</div>
<div>
<b>
<h3>
Show me your interest.
</h3>
</b>
<Input size="large" value={textBox4} onChange={this.handleChangeTextBox3} />
</div>
</div>
<div>
<Button type="danger" className="button" onClick={this.handleSubmit} >Click Here</Button>
</div>

</form>
}

</div>

<div>

{isShowResult &&
<div>
<App data={allData} />
</div>
}
</div>
</div>
);
}
}

app.js 文件

import React from 'react';
import { Row, Col } from 'antd';
export const languages = [

'/assets/tech logos/Languages/Javascript/angular js.png',
'/assets/tech logos/Languages/JAVA/java-logo.jpg',
'/assets/tech logos/Languages/Javascript/react js.png',
'/assets/tech logos/Languages/Javascript/node js.png',
'/assets/tech logos/Languages/Microsoft/AspDotNet.png',
'/assets/tech logos/Languages/Python/Python.png',
'/assets/tech logos/Languages/C++.png',

];

export const frameWorks = [
'/assets/tech logos/IDE/Eclipse.png',
'/assets/tech logos/IDE/IntelliJ_IDEA_Logo.svg.png',
'/assets/tech logos/IDE/visual_studio_purple (1).png',
];

export const buildSystem = [
'/assets/tech logos/Build servers/apache ant.png',
'/assets/tech logos/Build servers/bamboo-darkbluetext_medium_trans.png',
'/assets/tech logos/Build servers/gitlab.png',
'/assets/tech logos/Build servers/maven.png',
'/assets/tech logos/Build servers/vsts-2015.png',
];

export const bugTracking = [
'/assets/tech logos/Bugtracking/bugzilla.png',
'/assets/tech logos/Bugtracking/jira.png',
'/assets/tech logos/Bugtracking/Micro focus ALM.jpeg',
];

export default function APP(props) {

const languagesmap = props.data.map(languages => <img src={languages} alt={languages} className="image-size" />);
const frameWorksmap = props.data1.map(frameWorks => <img src={frameWorks} alt={frameWorks} className="image-size" />);
const buildSystemmap = props.data2.map(buildSystem => <img src={buildSystem} alt={buildSystem} className="image-size" />);
const bugTrackingmap = props.data3.map(bugTracking => <img src={bugTracking} alt={bugTracking} className="image-size" />);

return (
<div>
<center>
<h1> <div className="text-color"> <b>MS SOFTWARE</b> </div> </h1>
{/* <b>you selecte {props.data} </b> */}

</center>
<div className="gutter-example">

<Row type="flex" justify="center" gutter={0}>

<Col className="gutter-row " span={4}>
<div className="gutter-box border-right-upper">
<div>
<Row>

<Col className="text" span={24}>
<center> <b> Tech Stack </b> </center>
</Col>

</Row>

<Row>

<Col span={24}>
{/* <img src={languagesmap} alt={languagesmap} className="image-size"/> */}
{languagesmap}
</Col>

</Row>
</div>

</div>
</Col>

<Col className="gutter-row " span={4}>
<div className="gutter-box border-right-upper">
<div>
<Row>

<Col className="text" span={24}>

<center> <b> IDE/STACK </b> </center>

</Col>

</Row>

<Row>

<Col span={24}>
{/* <img src={props.data1} alt={props.data1} className="image-size"/> */}
{frameWorksmap}
</Col>

</Row>

</div>

</div>
</Col>

<Col className="gutter-row" span={4}>
<div className="gutter-box border-right-upper">
<div>
<Row>

<Col className="text" span={24}>

<center> <b> Build System </b> </center>
</Col>

</Row>

<Row>
<Col span={24}>
{/* <img src={props.data2} alt={props.data2} className="image-size"/> */}
{buildSystemmap}
</Col>

</Row>
</div>
</div>
</Col>

<Col className="gutter-row" span={4}>
<div className="gutter-box border">
<div>
<Row>

<Col className="text" span={24}>
<center> <b> Bug Tracking </b> </center>
</Col>

</Row>

<Row>

<Col span={24}>
{/* <img src={props.data3} alt={props.data3} className="image-size"/> */}
{bugTrackingmap}
</Col>

</Row>
</div>
</div>
</Col>

</Row>

<Row type="flex" justify="center" gutter={0}>

<Col className="gutter-row " span={4}>
<div className="gutter-box border-right-lower">
<Col span={4} />
<Col span={16}>
<b>
{props.textData1}
</b>
</Col>
<Col span={4} />
</div>
</Col>

<Col className="gutter-row " span={4}>
<div className="gutter-box border-right-lower">
<Col span={3} />
<Col span={18}>
<b>
{props.textData2}
</b>
</Col>
<Col span={3} />
</div>
</Col>

<Col className="gutter-row " span={4}>
<div className="gutter-box border-right-lower">
<Col span={4} />
<Col span={16}>
<b> {props.textData3} </b>
</Col>
<Col span={4} />
</div>
</Col>

<Col className="gutter-row " span={4}>
<div className="gutter-box">
<Col span={4} />
<Col span={16}>
<b> {props.textData4} </b>
</Col>
<Col span={4} />
</div>
</Col>

</Row>

</div>
</div >
);
}

如果我必须在 setState 函数中使用对象和扩展运算符,应该怎么做:

handleChangeTextBox1(e){ 
this.setState({
allData:(this.state.textBox,{textBox:e.target.value} )
});
console.log(e.target.value) }

这里,allData 是一个具有对象textBox 的函数。 textBox 是数据输入的输入区域。我使用不同的句柄更改事件和不同的 this.setState 值完成了这个示例。我如何在一个 setState 中使用它?

我试过的旧版本代码是这样的:

handleChangeTextBox(e){ this.setState({allData:{textBox: e.target.value}}); console.log(e.target.value) }

但是这里一个文本框中的数据更改会更改所有句柄更改事件的数据。

最佳答案

您需要识别从状态到数据的文本映射,为此您可以存储与 textBox 的 id 映射的值

state = {
allData: {}
}

handleChangeTextBox(e){
this.setState(prevState => ({
allData:{
...prevState.allData,
[e.target.name]: e.target.value
}
}));
}

在此之后,您可以像这样设置文本框值

<input name="inp1" value={this.state.allData["inp1"]} onChange={this.handleChangeTextBox}/>

关于javascript - 使用单个事件处理程序更改多个文本框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51017244/

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