admin管理员组

文章数量:1313006

does anybody know how to retrieve the "key" value from a select when the function onChange is triggered?

callback(e) {
  e.target.value;
}
...
<select onChange={this.callback.bind(this)}>
    <option key={obj.id}>{obj.name}</option>
    <option key={obj.id}>{obj.name}</option>
</select>

I know I can get the value of obj.name with target.value but I can't find out how to retreive the key.

does anybody know how to retrieve the "key" value from a select when the function onChange is triggered?

callback(e) {
  e.target.value;
}
...
<select onChange={this.callback.bind(this)}>
    <option key={obj.id}>{obj.name}</option>
    <option key={obj.id}>{obj.name}</option>
</select>

I know I can get the value of obj.name with target.value but I can't find out how to retreive the key.

Share Improve this question asked Jul 28, 2016 at 2:08 G. M.G. M. 1,90514 silver badges12 bronze badges 3
  • why would you want the key? that for React to keep track of the order, if you need obj.id, just pass in id={obj.id} and access it that way – omarjmh Commented Jul 28, 2016 at 2:12
  • I thought about passing the id. I'd do something like callback.bind(this, obj.id) . Problem is I generate the option tags with an array.map and I can't access directly obj.id – G. M. Commented Jul 28, 2016 at 2:52
  • 1 Also I'd like to know why people are downvoting this question, since so far none but Danny managed to find an answer and even that one isn't that trivial... – G. M. Commented Jul 28, 2016 at 11:18
Add a ment  | 

5 Answers 5

Reset to default 2

You could set the id to a data attribute, then access it from e.target. A modified version of your example:

callback(e) {
  for (let node of e.target.children) {
    if (node.value === e.target.value) {
      console.log(node.getAttribute('data-id');
      return;
    }
  }
}
...
<select onChange={this.callback.bind(this)}>
    <option key={obj1.id} data-id={obj1.id} value={obj1.value}>{obj1.name}</option>
    <option key={obj2.id} data-id={obj2.id} value={obj2.value}>{obj2.name}</option>
</select>

Edit:

Modified above example to fix issue, since e.target actually refers to the select element, not the option element. You need to find the selected option then get its data-id attribute. Working example:

https://jsfiddle/69z2wepo/50706/

callback(e) {
  e.target.value; // Changed value
  e.target.selectedIndex; // Index of the change value
}

I know this question is older than my children, but a mix of the two answers above: since you're likely generating the <option> elements via props, you could use the select element's selectedIndex value, then index into your props to get the object used to generate the option in the first place.

function DropDown({ current, options, onChange }) {
  return (
    <select value={current} onChange={(event) => {
      const { selectedIndex } = event.target;
      onChange(options[selectedIndex]);
    }}>
      {options.map(({ value, key }) => (<option key={key}>{value}</option>))}
    </select>
  );
}

<DropDown
  current={'ABC'}
  options={[
    { value: 'ABC', key: 'key:abc' },
    { value: 'IJK', key: 'key:ijk' },
    { value: 'XYZ', key: 'key:xyz' },
  ]}
  onChange={(obj) => {
    console.log('Changed to object', { obj });
  }}
/>

I don't know about reactjs, but retrieving the selected value in javascript is:

element.options[element.selectedIndex].value

So maybe try:

element.options[element.selectedIndex].key

Where element is your actual select.

The best way to get the key and label in the onChange event of select in React is:

console.log('Label 

本文标签: javascriptReacts select onChange get keyStack Overflow