如何在ReactJs中获取select标记的值

时间:2016-05-09 10:00:37

标签: javascript reactjs

我正在建立一个像这样的表格

 var TableforbasictaskForm = React.createClass({

    getInitialState: function() {
        return {
            taskName: '',
            description: '',
            empComment: '',
            emprating: ''
        };
    },
    handletaskNameChange: function(e) {
        this.setState({
            taskName: e.target.value
        });
    },
    handledescriptionChange: function(e) {
        this.setState({
            description: e.target.value
        });
    },
    handleempCommentChange: function(e) {
        this.setState({
            empComment: e.target.value
        });
    },
    handleempratingChange: function(e) {
        this.setState({
            emprating: e.target.selected
        });
    },

    render: function() {
        return ( < div className = "row margin-top" >
            < form className = "col-md-12" >
            < div className = "col-md-2" >
            < input className = "form-control "
            type = "text"
            placeholder = "Task name"
            value = {
                this.state.taskName
            }
            onChange = {
                this.handletaskNameChange
            }
            /> < /div> < div className = "col-md-3" >
            < textarea className = "form-control"
            name = "description"
            placeholder = "Standard Discription of task"
            value = {
                this.state.description
            }
            onChange = {
                this.handledescriptionChange
            }
            /> < /div> < div className = "col-md-3" >
            < textarea className = "form-control"
            name = "empComment"
            placeholder = "Employee Comment"
            value = {
                this.state.empComment
            }
            onChange = {
                this.handleempCommentChange
            }
            /> < /div>

            < div className = "col-md-2" >
            < select value = {
                optionsState
            }
            className = "form-control"
            name = "emprating"
            onChange = {
                this.handleempratingChange
            } >
            < option value = "" > Employee Ratings < /option> < option value = "1" > 1 < /option> < option value = "2" > 2 < /option> < option value = "3" > 3 < /option> < option value = "4" > 4 < /option> < option value = "5" > 5 < /option> < /select> < /div> < div className = "col-md-2" >
            < input className = "form-control"
            type = "submit"
            value = "Post" / >
            < /div> < /form> < /div>
        );
    }
});

所以,我想知道如何定义Select标记,以便我可以将所选选项的值加载到emprating变量中。

我当前的代码无法正常运行。

1 个答案:

答案 0 :(得分:0)

您可以使用ref从React中的输入标记访问值。

let nodeRef;

<div className="col-md-2">
   <input 
     className="form-control " 
     type="text" 
     placeholder="Task name" 
     ref = { node => {
          nodeRef = node;
        }}
     value={this.state.taskName}
     onChange={this.handletaskNameChange}
   />
</div>

现在您可以使用此nodeRef变量来获取值。有关详细信息,请参阅此处:https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute