陣營參考文獻我有以下JSX:捕獲由事件
import React, {Component} from 'react';
class Register extends Component {
handleSubmit(){
console.log("hey!")
}
setHandles(c){
//This never executes.
console.log("foo");
}
render() {
return (
<form className='form-horizontal' onSubmit={this.handleSubmit}>
<h4>Create a new account.</h4>
<hr />
<div className="form-group">
<label htmlFor="Email" className="col-md-2 control-label">User Name</label>
<div className="col-md-10">
//********************************
//**** USING SETHANDLES HERE *****
//********************************
<input type="email" className="form-control" ref="{this.setHandles}" />
<span className="text-danger"></span>
</div>
</div>
<div className="form-group">
<label htmlFor="Password" className="col-md-2 control-label">Password</label>
<div className="col-md-10">
//********************************
//**** USING SETHANDLES HERE *****
//********************************
<input type="password" className="form-control" ref="{this.setHandles}" />
<span className="text-danger"></span>
</div>
</div>
...
我setHandles
功能永遠不會執行。爲什麼?
我的意圖是給每個input
屬性ref="{this.setHandles}"
以便setHandles
回調可以註冊每個對應的DOM元素。之後,當我準備發佈表單時,我可以遍歷DOM元素數組以獲取相應的輸入值。
刪除引號。嘆。這正是我厭倦編程時所得到的結果。另外,非常感謝泛型onChange想法。 –