gpt4 book ai didi

blazor - EditForm - 如何防止按回车键提交

转载 作者:行者123 更新时间:2023-12-03 21:54:15 25 4
gpt4 key购买 nike

我找到了 this article但我很难理解如何防止任何 <input> 独立提交“输入”键

<EditForm Model="exampleModel" OnValidSubmit="HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />

<InputText id="name" @bind-Value="exampleModel.Name" />
<InputText id="name2" @bind-Value="exampleModel.Name2" />

<button type="submit">Submit</button>
</EditForm>

@code {
private ExampleModel exampleModel = new ExampleModel();

private void HandleValidSubmit()
{
Console.WriteLine("OnValidSubmit");
}

public class ExampleModel
{
[Required]
[StringLength(10, ErrorMessage = "Name is too long.")]
public string Name { get; set; }
public string Name2 {get; set;}
}
}

用例

Enter key On HTML forms if you are filling out a text box and press the enter key it will submit the form, even if you haven't finished filling in the rest of the information. There are many websites which use this feature such as Google search box will submit when you press the enter key. This works because you only have one text box to fill out, but if there are more than one field to fill in you don't want the form to submit on the enter key.

最佳答案

Leonardo Lurci,这是一个完全用 C# 实现的完整解决方案,没有 JSInterop。事实证明,微软已经提供了这个功能,但是他们没有提供足够的示例来演示如何使用它。
事实证明,我不能使用这对 @onkeypress="@KeyHandler"@onkeypress:preventDefault使用诸如 InputText 之类的 Forms 组件,但是将这些指令应用于 Html 标记是可行的并且运行良好。例如,请参阅我如何将这些指令应用于“提交”按钮。
因此,我将基类 InputBase 子类化,这是 InputText 组件派生自的类,通过添加一个输入元素来覆盖默认 View 呈现,我可以向其中添加新功能的指令。
TextBox.razor(这个来代替 InputText)


@inherits InputBase<string>

<input type="text" value="@CurrentValueAsString" id="Id" class="@CssClass"
@onkeydown="KeyDownHandler" @onkeypress="KeyPressHandler"
@onkeypress:preventDefault/>

@code{

protected override bool TryParseValueFromString(string value, out string
result, out string validationErrorMessage)
{
result = value;
validationErrorMessage = null;
return true;
}

void KeyDownHandler(KeyboardEventArgs args)
{
if (args.Key == "Backspace" && CurrentValueAsString.Length >=1)
{
CurrentValueAsString = CurrentValueAsString.Substring(0,
CurrentValueAsString.Length - 1);
}
}

void KeyPressHandler(KeyboardEventArgs args)
{

if (args.Key == "Enter")
{
return;
}
var key = (string)args.Key;
CurrentValueAsString += key;
}
}
用法

<p>Leave me a comment</p>

<EditForm Model="Model" OnValidSubmit="HandleValidSubmit" >
<DataAnnotationsValidator />

<div class="form-group">
<label for="name">Name: </label>
<TextBox Id="name" Class="form-control" @bind-Value="@Model.Name" >
</TextBox>
<ValidationMessage For="@(() => Model.Name)" />

</div>
<div class="form-group">
<label for="body">Text: </label>
<InputTextArea Id="body" Class="form-control" @bind-Value="@Model.Text" >
</InputTextArea>
<ValidationMessage For="@(() => Model.Text)" />
</div>
<p>
<button type="submit" @onkeypress="KeyHandler" @onkeypress:preventDefault>
Submit
</button>
</p>
</EditForm>


@code
{
private Comment Model = new Comment();

private void HandleValidSubmit()
{
Console.WriteLine("Submit...");
}


void KeyHandler(KeyboardEventArgs args)
{
if (args.Key == "Enter")
{
return;
}
}

public class Comment
{
public string Name { get; set; } = "Jeff";
public string Text { get; set; } = "I'm Jeff. I'm from Canada";
}

}
请不要犹豫,提出任何问题
希望这可以帮助...

关于blazor - EditForm - 如何防止按回车键提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62054672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com