我想从我的选择选项列表中获取属性。
同时我的代码如下:
var Billing = React.createClass({
getInitialState() {
return {
associations: [],
value: '',
associationsList: '1'
};
},
handleChange(event) {
this.setState({value: event.target.value});
},
handleOption(event){
var option = event.target.getAttribute('data-id');
this.setState({associationsList: option});
},
render() {
var listAssociations = this.state.associations.map(function(index){
return (
<option onChange={this.handleOption} value={index.name} data-id={index.id} ref={index.name} key={index.id}>{index.name}</option>
)
});
var BillingInfo
{
if(this.state.associationsList == "0")
{
return (
<div>
<Tabs selected={0}>
<Pane label="Billing Info">
<div className="row">
<div className="small-12">
Associations:
<select value={this.state.value} onChange={this.handleChange}>
{listAssociations}
</select>
</div>
</div>
<div>
{this.state.value}<br/>
{this.state.associationsList}
Basic package
</div>
</Pane>
<Pane label="Billing History">
<div>Billing history</div>
</Pane>
</Tabs>
</div>
);
我用“index.name”定义了选项值,我想检索“data-id”以获得不同的输出。有人可以帮助我吗?
已编辑
我已经更新了我的代码,如下所示。但是我仍然无法获得数据 ID 或者是否有任何其他方法可以在不使用“id”设置“值”的情况下获取我的数据 ID。
handleChange(event) {
var index = event.target.selectedIndex;
var optionElement = event.target.childNodes[index]
var option = optionElement.getAttribute('data-id');
this.setState({
associationsList: option,
value: event.target.value
});
},
render() {
var listAssociations = this.state.associations.map(function(index){
return (
<option value={index.name} data-id={index.id} ref={index.name} key={index.id}>{index.name}</option>
)
});
var BillingInfo
{
if(this.state.associationsList == "0")
{
return (
<div>
<Tabs selected={0}>
<Pane label="Billing Info">
<div className="row">
<div className="small-12">
Associations:
<select value={this.state.value} onChange={this.handleChange}>
{listAssociations}
</select>
</div>
</div>
<div>
{this.state.value}<br/>
{this.state.associationsList}
Basic package
</div>
</Pane>
<Pane label="Billing History">
<div>Billing history</div>
</Pane>
</Tabs>
</div>
);
}