使用 @ref 将焦点放在动态生成的 InputNumber 上

Set focus on dynamic generated InputNumber by using @ref

您可以动态处理引用:

@foreach (var t in PotatosAndReferences)
{
    <input type="text" @key="@t.Potato.Id" @bind="t.Potato.Name" @ref="@t.Ref" />
}
<br>
<button @onclick="_ => GoN(0)">Focus first</button>
<button @onclick="_ => GoN(15)">Focus 16</button>
@code {
    public class Potato
    {
        public int Id {get; set; }
        public string Name {get; set;} = default!;
    }  
    public class PotatoAndReference
    {
        public Potato Potato {get; set;} = default!;
        public ElementReference? Ref {get; set;} = null;
    }
    public List<Potato> Potatos
        = 
        Enumerable.Range(1, 50)
        .Select(i => new Potato() {Id = i, Name = $"Potato {i}"}).ToList();
    public List<PotatoAndReference> PotatosAndReferences = default!;
    protected override void OnInitialized()
        =>
        PotatosAndReferences =
            Potatos
            .Select(p => new PotatoAndReference(){Potato = p}).ToList();
    protected async Task GoN(int n)
        =>
        await PotatosAndReferences.ElementAt(n).Ref!.Value.FocusAsync();    
}