Elm Html.Events-将输入值传递给onBlur消息 [英] Elm Html.Events - Pass input value to onBlur message

查看:57
本文介绍了Elm Html.Events-将输入值传递给onBlur消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种输入:

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

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

which creates Msgs.ChangeName for every single character added to input.

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

I would prefer to update model after user leaves input but onBlur doesn't have any payload about input:

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

以上代码不会以结尾结尾错误:

Above code doesn't compile ending with error:

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.


推荐答案

您可以在模糊处理程序上创建一个变体会像这样拉出 target.value

You can create a variation on the "blur" handler which pulls out target.value like this:

import Html.Events exposing (on, targetValue)
import Json.Decode as Json

onBlurWithTargetValue : (String -> msg) -> Attribute msg
onBlurWithTargetValue tagger =
    on "blur" (Json.map tagger targetValue)

这篇关于Elm Html.Events-将输入值传递给onBlur消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆