【发布时间】:2015-09-06 17:00:22
【问题描述】:
我有一个带有 3 个选项的 <select> HTML 元素和一个 <p> 元素。在<p> 元素中,我想打印<select> 中当前选定项目的索引。例如。如果我选择第一个选项,它应该打印 0,如果我选择第二个选项,它应该打印 1,依此类推。如何从下面给出的最小代码开始?
import Html as H exposing (Html)
import Maybe
import Signal as S exposing (Address, (<~))
type alias Model = { selected : Maybe Int }
model = { selected = Nothing }
type Action = NoOp | Select Int
update action model =
case action of
NoOp -> model
Select n -> { model | selected <- Just n }
view address model =
H.div []
[ H.select [] [ H.option [] [ H.text "0" ]
, H.option [] [ H.text "1" ]
, H.option [] [ H.text "2" ]
]
, H.p [] [ H.text <| Maybe.withDefault ""
<| Maybe.map toString model.selected ]
]
actions = Signal.mailbox NoOp
main = view actions.address <~ S.foldp update model actions.signal
【问题讨论】:
标签: html-select selectedindex elm