我有以下代码。
<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 null ab a abc ab abcd 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/