Javascript 如何让用户能够按“;输入”;使用react进行搜索?
我想通过单击ENTER键添加搜索功能。如何将其添加到代码中? 这是我的项目中的一个编解码器Javascript 如何让用户能够按“;输入”;使用react进行搜索?,javascript,reactjs,Javascript,Reactjs,我想通过单击ENTER键添加搜索功能。如何将其添加到代码中? 这是我的项目中的一个编解码器 import React, { Component } from 'react'; import './SearchBar.css'; class SearchBar extends Component { constructor(props) { super(props); this.search = this.search.bind(this);
import React, { Component } from 'react';
import './SearchBar.css';
class SearchBar extends Component {
constructor(props) {
super(props);
this.search = this.search.bind(this);
this.handleTermChange = this.handleTermChange.bind(this);
}
handleTermChange(event) {
const newTerm = event.target.value;
this.setState = ({ term: newTerm});
}
search() {
this.props.onSearch(this.state.term);
}
render() {
return(
<div className="SearchBar">
<input placeholder="Enter A Song, Album, or Artist" onChange = {this.handleTermChange}/>
<a onClick ={this.search}>SEARCH</a>
</div>
)}
}
export default SearchBar;
import React,{Component}来自'React';
导入“/SearchBar.css”;
类搜索栏扩展组件{
建造师(道具){
超级(道具);
this.search=this.search.bind(this);
this.handleTermChange=this.handleTermChange.bind(this);
}
handleTermChange(事件){
const newTerm=event.target.value;
this.setState=({term:newTerm});
}
搜索(){
this.props.onSearch(this.state.term);
}
render(){
返回(
搜寻
)}
}
导出默认搜索栏;
React实现输入的onKeyUp
方法
<input placeholder="Enter A Song, Album, or Artist" onChange={this.handleTermChange} onKeyUp={this.handleKeyPress.bind(this)}/>
React执行输入的
onKeyUp
方法
<input placeholder="Enter A Song, Album, or Artist" onChange={this.handleTermChange} onKeyUp={this.handleKeyPress.bind(this)}/>