【发布时间】:2016-10-14 13:03:45
【问题描述】:
我见过what is required for a getting the selected index of a single select,但我有兴趣从多选中获取所有选定的选项。我无法弄清楚如何做到这一点。
我尝试了以下操作,但我怀疑 Json 解码器失败了。不过我不是 100% 确定这一点,因为解码发生在虚拟 dom 代码中,并且其中的任何错误都会被丢弃。
type Msg
= SetMultipleInts (List Int)
-- I'm not seeing the SetMultipleInts message when I click on the multiselect
view model =
div []
[ select (onSelect SetMultipleInts) (List.map myOption [1..4]) ]
myOption : Int -> Html Msg
myOption id =
option [ value (toString id) ] [ text <| "Option " ++ (toString id) ]
-- I'm not seeing anything happen in the onchange
onMultiSelect : (List Int -> msg) -> List (Html.Attribute msg)
onMultiSelect msg =
[ on "change" (Json.map msg targetSelectedOptions), multiple True ]
targetSelectedOptions : Json.Decoder (List Int)
targetSelectedOptions =
Json.at [ "target", "selectedOptions" ] (Json.list (Json.at [ "value" ] Json.int))
我可以在不使用端口的情况下执行此操作吗?
【问题讨论】:
-
onMultSelect 应该在哪里使用?它已定义且未使用。
标签: html-select elm