简体   繁体   中英

Pass value of <select> tag in React.js

Beginner question, but I'm trying to run a test to see how to pass the value of the selected option. Currently trying to console log the selected option's value, in this case value='2' or value='4'. Any advice or leads to the right direction would be greatly appreciated.

testUpdate = selectedValue => {
    console.log(selectedValue);
};

render() {

    return (
        <select
            className='new-lead-dropdown'
            onChange={this.testUpdate}

            <option defaultValue value='1'>
                    Responses
            </option>
            <option value='2'>Phone</option>
            <option value='4'>Screen</option>
            <option value='5'>Interview</option>
            <option value='6'>Offer</option>
            <option value='15'>Hired</option>
        </select>

You can do something like this:

You have to grab the current event during onChange and retrieve the value. Hope this helps.

 <select onChange={(e) => this.testUpdate({ value: e.target.value })}>
    <option defaultValue value='1'>Responses
    <option value="2">Phone</option>
    <option value="4">Screen</option>
    <option value='5'>Interview</option>
    <option value="6">Offer</option>
    <option value="15">Hired</option>
</select>
testUpdate = selectedValue => {
  console.log(selectedValue.target.value);
};

Change,

onChange={this.testUpdate}

to

onChange={e => this.testUpdate(e.target.value)}

Here you need to make sure that you are passing down the selected value like e.target.value in testUpdate function.

Working Sandbox

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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