如何在 Blazor WebAssembly 中将输入框值设置为字符串?

How to set input box value to a string in Blazor WebAssembly?

我正在使用 Blazor WebAssmebly。我有一个输入框,我只是想在用户输入文本并按下回车键后将其重置为无文本:

private void onChange(Microsoft.AspNetCore.Components.ChangeEventArgs args)
{
    value = (string)args.Value;
}

public void Enter(KeyboardEventArgs e)
{
    if (e.Code == "Enter" || e.Code == "NumpadEnter")
    {
        if (value.Trim() != "")
        {
            doSomething();
        }
    }
}

所以我将变量'value'设置为输入文本,但是我想清除文本框。我该怎么做?

您似乎没有将输入绑定到 value 变量。您的代码应该是这样的:

 <input id="txtWord" name="txtWord" placeholder="Enter your text" 
  value ="@value" @onchange="onChange" @onkeyup="Enter" />

@code
{
    private string value;
}

请注意,通过将 value 属性添加到输入元素,我创建了双向数据绑定,从变量到控件,以及从控件到变量。当您使用 @onchange 指令时,您创建了一个单向数据绑定。

为了重置输入元素,您可以执行以下操作:

if (value.Trim() != "")
{
     // I guess it is here that you want to reset the input 
     // element. Assigning empty string to the `value` variable
     // will cause the control to re-render with the new value; 
     // that is empty string...
     value = "";
     doSomething();
}

这将处理“输入”和“提交”按钮的按下操作。我在我正在开发的 SignalR 库中使用它。默认 css 类 用于 Bootstrap.

SendBox.razor

<EditForm Model="@SendBoxViewModel" OnSubmit="@Send">

    <div class="@DivClass">

        <input @ref="@inputBox"
               @bind-value="SendBoxViewModel.InputMessage"
               @bind-value:event="oninput"
               type="text"
               aria-label="@Placeholder"
               placeholder="@Placeholder"
               class="@InputClass"
               aria-describedby="button-send"
               disabled=@Disabled>

        <button class="@ButtonClass"
                type="submit"
                id="button-send"
                disabled=@Disabled>
            @Label
        </button>

    </div>

</EditForm>

SendBox.razor.cs

public partial class SendBox : ComponentBase
{
    private ElementReference inputBox;

    [Parameter]
    public string Label { get; set; } = "Send";

    [Parameter]
    public string Placeholder { get; set; } = "Type a new message here.";

    [Parameter]
    public string DivClass { get; set; } = "input-group";

    [Parameter]
    public string InputClass { get; set; } = "form-control";

    [Parameter]
    public string ButtonClass { get; set; } = "btn btn-outline-primary";

    [Parameter]
    public bool Disabled { get; set; }

    [Parameter]
    public EventCallback<string> OnSend { get; set; }

    public SendBoxViewModel SendBoxViewModel { get; set; } = new SendBoxViewModel();

    private bool MessageInputInvalid => string.IsNullOrWhiteSpace(SendBoxViewModel.InputMessage);
    private async Task Send()
    {
        if (!MessageInputInvalid)
        {
            await OnSend.InvokeAsync(SendBoxViewModel.InputMessage);
            SendBoxViewModel.InputMessage = string.Empty;
            await inputBox.FocusAsync();
        }
    }
}

SendBoxViewModel.cs

public class SendBoxViewModel
{
    [MinLength(length: 1)]
    [MaxLength(length: 1024)]
    [Required(AllowEmptyStrings = false)]
    public string? InputMessage { get; set; }
}