【问题标题】:Elm Html.Events - Pass input value to onBlur messageElm Html.Events - 将输入值传递给 onBlur 消息
【发布时间】:2017-03-19 23:49:01
【问题描述】:

我有这样的输入:

inputName player =
    input
        [ type_ "text"
        , onInput (Msgs.ChangeName player)
        , value player
        ]

为添加到输入的每个字符创建 Msgs.ChangeName。

我希望在用户离开输入后更新模型,但 onBlur 没有任何关于输入的有效负载:

inputName player =
    input
        [ type_ "text"
        , onBlur (Msgs.ChangeName player)
        , value player
        ]

以上代码无法编译并以错误结尾:

The 1st entry has this type:

    Html (String -> Msg)

But the 2nd is:

    Html (Msg)

Hint: It looks like a function needs 1 more argument.

【问题讨论】:

    标签: elm


    【解决方案1】:

    您可以在“模糊”处理程序上创建一个变体,它会像这样提取target.value

    import Html.Events exposing (on, targetValue)
    import Json.Decode as Json
    
    onBlurWithTargetValue : (String -> msg) -> Attribute msg
    onBlurWithTargetValue tagger =
        on "blur" (Json.map tagger targetValue)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 2017-08-16
      • 2012-10-03
      • 1970-01-01
      相关资源
      最近更新 更多