任务是将输入的每个字符传递给handleExchange方法。在此方法中,将发生对输入值的处理。
如何将输入标签传递给handleExchange方法的参数?
function App() {
function handleExchange(value) {
console.log(value)
}
return (
<input className = "input-r" placeholder = "Type here" />
)
}
ReactDOM.render(<App /> , document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
我试图这样做:
function App() {
function handleExchange(value) {
console.log(value);
}
return (
<div>
<input className="input-r" placeholder="Type here" onKeyPress={handleExchange(this.value)} />
{/*or*/}
{/* onKeyPress={handleExchange(event) */}
</div>
)
}
ReactDOM.render(<App />, document.getElementById("root"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
慕的地6264312
幕布斯7119047
相关分类