问题
I have this dropdownList:
<Dropdown
isOpen={this.state.dropdownOpen[3]}
toggle={() => {
this.toggle(3);
}}
>
<DropdownToggle className="my-dropdown" caret>
{" "}
{this.state.dropDownValue}
</DropdownToggle>
<DropdownMenu>
<DropdownItem productid="0">
{" "}
<div onClick={this.changeValue}>
PURE BLACK MAT BLUETOOTH
</div>
</DropdownItem>
<DropdownItem productid="1">
{" "}
<div onClick={this.changeValue}>
PLAYER BLACK MAT FILAIRE
</div>
</DropdownItem>
<DropdownItem productid="2">
{" "}
<div onClick={this.changeValue}>
PURE PLUS BLACK MAT MEMORY 128 G
</div>
</DropdownItem>
<DropdownItem productid="3">
{" "}
<div onClick={this.changeValue}>
PURE LIMITED CHROME GOLD MEMORY 256G
</div>
</DropdownItem>
<DropdownItem productid="4">
{" "}
<div onClick={this.changeValue}>
PURE GOLD MAT BLUETOOTH
</div>
</DropdownItem>
<DropdownItem productid="5">
{" "}
<div onClick={this.changeValue}>
PLAYER GOLD MAT FILAIRE
</div>
</DropdownItem>
<DropdownItem productid="6">
{" "}
<div onClick={this.changeValue}>
PURE PLUS GOLD MAT MEMORY 128 G
</div>
</DropdownItem>
<DropdownItem productid="7">
{" "}
<div onClick={this.changeValue}>GAME ONE WHITE</div>
</DropdownItem>
</DropdownMenu>
</Dropdown>
And this onClickListener:
changeValue(e) {
console.log(
"The selected productId is: ",
e.currentTarget.getAttribute("productid")
);
//The selected productId is: null
this.setState({
dropDownValue: e.currentTarget.textContent
});
}
I am trying to access the productid of the selected DropdownItem. But, for some reason the result is always null:
console.log( "The selected productId is: ", e.currentTarget.getAttribute("productid") ); //The selected productId is: null
Any idea what I've done wrong?
回答1:
I have fixed this by passing the attribute productid inside the div:
<DropdownItem>
{" "}
<div productid="0" onClick={this.changeValue}>
PURE BLACK MAT BLUETOOTH
</div>
</DropdownItem>
来源:https://stackoverflow.com/questions/60435037/reactstrap-accessing-the-id-of-the-selected-dropdownitem