0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ReactでSelectタグで現在の値を選択状態

Posted at

結論は、下記のように書く。

<select value={selected} onChange={(e:any) => setSelected(e.target.value)}>
    {getSelectItems.map( (getSelectItem:any) => (
        <option key={getSelectItem.item} value={getSelectItem.id}>
            {getSelectItem.item}
        </option>
    ))}    
</select>

ポイントは、selecタグに、"value"という要素を付与し、そこの値に現在の選択された値がくるようする。
この例では、変数selectedが現在の選択された値になっている。

optionタグのvalueが、selectタグのvalueの値と一致するとき、optionタグが選択(checked)
された状態になる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?