繁体   English   中英

如何在ReactJs中获取选择标签的值

[英]How to get the value of a select tag in 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变量中。

我当前的代码无法正常工作。

您可以使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM