Friday 15 May 2015

javascript - How to get selected value of a dropdown menu in ReactJS -


I am using feedback and I want to get the value of the selected option of a dropdown, but I do not have the technical Information. Any suggestions? Thanks! My dropdown is only one of the favorites:

  & lt; Id = "dropdown" & gt; & Lt; Option value = "N / A" & gt; N / A & lt; / Options & gt; & Lt; Option value = "1" & gt; 1 & lt; / Options & gt; & Lt; Option value = "2" & gt; 2 & lt; / Options & gt; & Lt; Option value = "3" & gt; 3 & lt; / Options & gt; & Lt; Option value = "4" & gt; 4 & lt; / Options & gt; & Lt; / Select & gt;    

The code render represents the component in the form given a given time. If you do, then the user will not be able to use form controls:

  & lt; Select value = "radish" & gt; & Lt; Option value = "orange" & gt; Orange & lt; / Options & gt; & Lt; Option value = "radish" & gt; Radish & lt; / Options & gt; & Lt; Option value = "cherry" & gt; Cherry & lt; / Options & gt; & Lt; / Select & gt;  

There are two solutions to working with form controls:

  1. Controlled components component state This gives the most control to reflect the user's selection, because whenever you see any changes made to state in the rendering of the component:

Example:

  Var Fruitsector = React. Sequet class ({getInitialState: function} {return {selectValue: 'radish}}}; Change handles: function (e) {this.setState ({selectValue: e.target.value});}, render: function () {Var message = 'You chose' + this.state.selectValue; Return (
gt; & lt; Select value = {this.state.selectValue} onChange = {this.handleChange}> gt; & lt; Option value = "Orange"> Orange Option Price = "Radish" & gt; Radish & lt; / option & gt; Option value = "cherry"> Cherry & lt; / Option & gt; & lt; / select & gt; & lt; p & gt; {message} & lt; / p & gt; & lt; / div & gt;);}}); Reactive.reader (& lt; name of fridge equalizer = "world" />, document.body); JSFiddle:
  1. Uncontrolled Components The second option is not to control the value and simply Respond to On Change In this case you can use the defaultValue prop to set the initial value.

      & lt; Div & gt; & Lt; Choose default default = {at this.state.selectValue} Change = {this.handleChange} & gt; & Lt; Option value = "orange" & gt; Orange & lt; / Options & gt; & Lt; Option value = "radish" & gt; Radish & lt; / Options & gt; & Lt; Option value = "cherry" & gt; Cherry & lt; / Options & gt; & Lt; / Select & gt; & Lt; P & gt; {Message} & lt; / P & gt; & Lt; / Div & gt; Documents for this are great: and also show how to work with multiple selections.  

    Update

    is to use and create a type of option 1 (using a controlled component). It contains the connect and the mapStateToProps function, which is easier than sound, but perhaps if you are just beginning, then maybe it's going to increase.


No comments:

Post a Comment