PreventDefault on Blazor input

前端 未结 2 785
南旧
南旧 2021-01-18 09:43

I have a simple application with an input field that should insert a predefined piece of text as you type.

The code I have looks like this:



        
2条回答
  •  失恋的感觉
    2021-01-18 10:40

    You can think a little differently in Blazor.

    Rather than using "bind" and preventing the keystroke, you can set the "value" and handle the "oninput" event, like this:

    https://blazorfiddle.com/s/azdn892n

    @page "/"
    

    Start typing something...

    @functions { private int _petitionCharStep = 0; private string _petition = "This is a dummy text"; public string PetitionInput { get; set; } = string.Empty; void PetitionHandleKeyDown(UIChangeEventArgs arg) { PetitionInput = _petition.Substring(0,++_petitionCharStep); Console.WriteLine(PetitionInput); } }

    I can't imagine why you would want to do this, and there are many extra things you need to do to cover backspaces, arrow keys, tab etc...

提交回复
热议问题