如何在Blazor中将焦点设置为文本框 [英] How do I set focus to a text box in Blazor

查看:87
本文介绍了如何在Blazor中将焦点设置为文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Blazor中将焦点设置为文本框?到目前为止,我们发现的唯一方法是使用JavaScript.

How do I set focus to a textbox in Blazor? So far the only way we have found is with JavaScript.

推荐答案

没有其他方法可以执行此操作...您可以使用JSInterop来执行此操作,如下所示:

There is no other way to do it... You can use JSInterop to do this, as follows:

 <input type="text" @ref="myref"/>

 @code {

    private ElementReference myref;
    [Inject] IJSRuntime JSRuntime { get; set; }

     protected override async Task OnAfterRenderAsync(bool firstRender)
    {
         if (firstRender)
        {
            await 
        JSRuntime.InvokeVoidAsync("exampleJsFunctions.focusElement", myref);
        }
   }
 }

JavaScript

<script>

    window.exampleJsFunctions =
    {
        focusElement: function (element) {
           element.focus();
        }
    };
</script>

希望这对您有帮助...

Hope this helps...

这篇关于如何在Blazor中将焦点设置为文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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