简体   繁体   English

如何选择<li>在 reactjs 中的按钮跨度中,下拉列表

[英]how selected <li> in button span in reactjs , dropdown

                            <div className="input-group-btn" data-dropdown>

                                <button className="btn btn-default dropdown-toggle" type="button"
                                        data-toggle="dropdown" aria-expanded="true">
                                    <span id="sortevent1" tabindex="0">1.select</span><span
                                    id="arrow" className="glyphicon glyphicon-align-center"></span>

                                </button>
                                <ul className="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
                                    <li role="presentation"><a role="menuitem" tabindex="1">1.select &nbsp;<span id="arrow-up"
                                                                className="glyphicon glyphicon-indent-right"></span></a>
                                    </li>
                                    <li role="presentation"><a role="menuitem" tabindex="2">2.select &nbsp;<span id="arrow-down"
                                                                className="glyphicon glyphicon-indent-left"></span></a>
                                    </li>
                                    <li role="presentation"><a role="menuitem" tabindex="3">3.select&nbsp;<span id="arrow-up"
                                                                className="glyphicon glyphicon-indent-right"></span></a>
                                    </li>

                                    <li role="presentation"><a role="menuitem" tabindex="4">4.select&nbsp;<span id="arrow-up"
                                                             className="glyphicon glyphicon-indent-right"></span></a>
                                    </li>

                                </ul>

                            </div>

How to select <li> and write button value(select button dropdown like select option) in reactjs?如何在reactjs中选择<li>并写入按钮值(选择按钮下拉菜单,如选择选项)?

Please help, I tried <li> inside onClick={this.userevent(this.target.tabindex)} and onChange but请帮忙,我在onClick={this.userevent(this.target.tabindex)}onChange尝试了<li>但是

userevent =(event)=>{

  console.log(event)
}

it didn't work it.它没有用。 How do I do with this code?我该如何处理此代码? Maybe use tabindex='1',2,3也许使用 tabindex='1',2,3

Use the onFocus() event, and consider providing the tabindex to the li itself.使用onFocus()事件,并考虑为li本身提供tabindex Also, make the test of the button dependent on a state variable, which you set on switching between the options.此外,使按钮的测试依赖于您在选项之间切换时设置的状态变量。 Here is a working example.这是一个工作示例。

<div className="input-group-btn" data-dropdown>

                            <button className="btn btn-default dropdown-toggle" type="button"
                                    data-toggle="dropdown" aria-expanded="true">
                                <span id="sortevent1" tabindex="0">{this.state.selected}</span><span
                                id="arrow" className="glyphicon glyphicon-align-center"></span>

                            </button>
                            <ul className="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
                                <li role="presentation"  tabindex="1" onFocus={(e)=>{this.setState({selected:e.target.value})}}><a role="menuitem"  >1.select &nbsp;<span id="arrow-up"
                                                            className="glyphicon glyphicon-indent-right"></span></a>
                                </li>
                                <li role="presentation"  tabindex="2" onFocus={(e)=>{this.setState({selected:e.target.value})}}><a role="menuitem"   onFocus={(e)=>{console.log(e.target.parentElement.value)}}>2.select &nbsp;<span id="arrow-down"
                                                            className="glyphicon glyphicon-indent-left"></span></a>
                                </li>
                                <li role="presentation" tabindex="3" onFocus={(e)=>{this.setState({selected:e.target.value})}}><a role="menuitem" >3.select&nbsp;<span id="arrow-up"
                                                            className="glyphicon glyphicon-indent-right"></span></a>
                                </li>

                                <li role="presentation" tabindex="4"  onFocus={(e)=>{this.setState({selected:e.target.value})}}><a role="menuitem" >4.select&nbsp;<span id="arrow-up"
                                                         className="glyphicon glyphicon-indent-right"></span></a>
                                </li>

                            </ul>

            </div>

These are changes I made:这些是我所做的更改:
1. Moved Tabindex to li from a. 1. 将 Tabindex 从 a 移动到 li。
2. Replaced the text in the button by this.state.selected 2.用this.state.selected替换按钮中的文字
3. added onFocus event-handlers to each li 3. 为每个 li 添加 onFocus 事件处理程序

You can refactor it further.您可以进一步重构它。 And do not forget to set the initial state in the constructor.并且不要忘记在构造函数中设置初始状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM