【发布时间】:2020-12-30 19:17:35
【问题描述】:
我正在尝试 Microsoft 的待办事项列表示例:https://docs.microsoft.com/en-us/aspnet/core/tutorials/build-a-blazor-app?view=aspnetcore-3.1
我想添加一个待办事项,而不是用鼠标点击按钮,我想按回车键。我不喜欢在这个解决方案中使用 JS:How to set the focus to an InputText element? 我尝试通过这行代码触发方法 private void Enter(KeyboardEventArgs e):
<button @onclick="AddTodo" @onkeypress="@(e=>Enter(e)" tabindex="0" >Add todo</button>
没用。
enter code here
<input placeholder="Something todo" @bind="newTodo" />
<button @onclick="AddTodo" @onkeypress="Enter" tabindex="0" >Add todo</button>
@code {
private IList<TodoItem> todos = new List<TodoItem>();
private string newTodo;
private void AddTodo()
{
if (!string.IsNullOrWhiteSpace(newTodo))
{
todos.Add(new TodoItem { Title = newTodo });
newTodo = string.Empty;
}
}
//private void Enter(KeyboardEventArgs e)
private void Enter()
{
//if (e.Key == "Enter")
{
if (!string.IsNullOrWhiteSpace(newTodo))
{
todos.Add(new TodoItem { Title = newTodo });
newTodo = string.Empty;
}
}
}
}
【问题讨论】:
标签: blazor keyboard-events blazor-webassembly