gpt4 book ai didi

javascript - Reactjs - 从 event.target 检索属性

转载 作者:行者123 更新时间:2023-11-29 17:44:01 24 4
gpt4 key购买 nike

我有一个呈现 Input type='select' 的组件:(我正在使用 reactstrap)

import React, {Component} from 'react'
import {
Form,
FormGroup,
Input,
Button,
Col,
} from 'reactstrap'
import {
withRouter,
} from 'react-router'
import Context from '../../../../../provider'

class NewPost extends Component {
constructor(props) {
super(props)
this.state = {
subreddits: [],
subreddit_selected: '',
subreddit_id: 0,
...
}
this.handleSubredditSelect = this.handleSubredditSelect.bind(this)
}

componentDidMount() {
fetch('/api/reddit/r/')
.then(data => data.json())
.then(json => {
this.setState({
subreddits: json,
...
})
})
}

handleSubredditSelect(event) {
console.log('selected id: ',event.target.id)
this.setState({
subreddit_selected: event.target.value,
subreddit_id: event.target.id,
}, () =>
this.props.history.push(`/${this.state.subreddit_selected}/new/`)
)
}

...

render() {
return (
<Context.Consumer>
{context => {
return (
<React.Fragment>
<Form
...
>
<FormGroup row>
<Col sm={7}>
<Input
type="select"
onChange={this.handleSubredditSelect}
required
>
<option key='0' disabled selected>Select an Option</option>
{this.state.subreddits.map((subreddit) => {
return (
<option key={subreddit.id} id={subreddit.id}>{'r/' + subreddit.name}</option>
)
})}
</Input>
</Col>
</FormGroup>
...
</React.Fragment>
)
}}
</Context.Consumer>
)
}
}

export default withRouter(NewPost)

所以,我有一个函数 handleSubredditSelect,它执行以下操作:

handleSubredditSelect(event) {  
this.setState({
subreddit_selected: event.target.value,
subreddit_id: event.target.id,
}, () =>
this.props.history.push(`/${this.state.subreddit_selected}/new/`)
)
}

在这个函数中,我没有得到 event.target.id 的任何值。

我也尝试了 event.target.key 但返回了一个空字符串“”。

我想将state 中的subreddit_id 设置为所选选项的ID

最佳答案

选择不起作用,因为 event.target<select>元素返回带有选项的整个树:

// result of event.target:
<select>
<option id="id-1" value="some1">some1</option>
<option id="id-2" value="some2">some2</option>
<option id="id-3" value="some3">some3</option>
</select>

而不是选择的那个。


用于从 select 访问当前选项元素你应该依靠selectedIndex :

event.target[event.target.selectedIndex].id 

代码:

export default class SelectForm extends React.Component {
constructor(props) {
super(props);
this.state = {
value: "some1",
id: "id-1"
};
}

handleChange = event => {
console.log("event", event.target, event.target.selectedIndex);

this.setState({
value: event.target.value,
id: event.target[event.target.selectedIndex].id
});
};

render() {
return (
<div>
<select value={this.state.sex} onChange={this.handleChange}>
<option id="id-1" value="some1">some1</option>
<option id="id-2" value="some2">some2</option>
<option id="id-3" value="some3">some3</option>
</select>
ID: {this.state.id}
</div>
);
}
}

Edit 9l814zo9yr

关于javascript - Reactjs - 从 event.target 检索属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51437307/

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