我有一个带有按钮(buttonAfter属性)的Input组件,我设置了一个与按钮相关联的onClick处理程序,因此用户可以键入一些文本并粘贴按钮来触发正确的操作.
我认为这个问题与React本身有关,而不是react-bootstrap.
看看这个有关React事件系统的一些基础知识:https://facebook.github.io/react/docs/events.html
当您使用onKeyDown时,onKeyPress或onKeyUp React将传递给您的处理程序一个使用以下属性的“target”对象的实例:
boolean altKey
数字charCode
…(全部见上面的链接)
所以你可以这样做:
- import React,{ PropTypes } from 'react';
- import ReactDOM from 'react-dom';
- import { Input } from 'react-bootstrap';
- class TestInput extends React.Component {
- handleKeyPress(target) {
- if(target.charCode==13){
- alert('Enter clicked!!!');
- }
- }
- render() {
- return (
- <Input type="text" onKeyPress={this.handleKeyPress} />
- );
- }
- }
- ReactDOM.render(<TestInput />,document.getElementById('app'));
我测试了上面的代码,它工作.我希望这对你有帮助.
再见