gpt4 book ai didi

c# - @onkeypress 没有获取文本输入框的更新值?

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:09 26 4
gpt4 key购买 nike

我有以下代码。

<input id="search" type="text" @bind="Search" @onkeypress="SearchChanged" />
@code {
string Search;

void SearchChanged()
{
var s = Search; // Search is always one character behind
}
}

在文本框中输入将触发函数 SearchChanged。但是,它得到的值始终是键入文本之前的一个字符。比如在SearchChanged中设置断点,

Typed Value of  Search===== ================a     nullab    aabc   ababcd  abc

BTW, the @onkeypress doesn't work in Internet Browser?


Tried to change the code to

<input id="search" type="text" @bind="Search" />
@code {
string search;
string Search
{
get { return search; }
set {
search = value;
ResultList = GetNewList(search).Result.ToList();
}
}

void SearchChanged()
{
var s = Search; // Search is always one character behind
}
}

我在 set { } 中设置了一个断点,但是,在输入文本框时,断点只命中了一两次?

最佳答案

您正在寻找@bind-value:event:

<input id="search" 
type="text"
@bind-value="Search"
@bind-value:event="oninput"
/>

@code {
string search;
string Search
{
get { return search; }
set {
search = value;
ResultList = GetNewList(search).Result.ToList();
}
}

引用 Data Binding文档:

In addition to handling onchange events with @bind syntax, a property or field can be bound using other events by specifying an @bind-value attribute with an event parameter (@bind-value:event).

关于c# - @onkeypress 没有获取文本输入框的更新值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58343192/

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