【问题标题】:Recognize when value of HTML input is changed识别 HTML 输入值何时更改
【发布时间】:2023-03-28 02:30:01
【问题描述】:

当输入(HTML)的值改变时,有没有办法调用方法?但不是当输入的焦点丢失时而是当我在 HTML 输入中输入字符时。

我使用@bind 将字段绑定到输入。

然后我尝试使用@onkeydown 和@onkeyup 来调用该方法。但是最后插入的字符还没有添加到有界字段中。

我也尝试过使用@onchange,但随后我会收到错误消息“属性 onchange 已为此元素使用了两次或多次。-我认为绑定也订阅了此事件。

我的最后一个想法是使用(完整)属性作为键,而不是字段。在属性的设置器中,我调用了该函数。但问题是,当 HTML 输入失去焦点时会调用 setter。

        <table class="table table-condensed table-hover" data-click-to-select="true">
            <thead class="custom-headline">
                <tr>
                    <th class="align-text-top">
                        @indexText.index_table_title<br />
                        <input type="text" @onkeydown="Test" @bind="titleSearch" />
                    </th>
                    ...

有人知道如何解决这个问题吗?

谢谢转发!

最好的问候 马蒂亚斯

【问题讨论】:

    标签: c# blazor


    【解决方案1】:

    你可以这样做:

    <input type="text" @bind-value:event="oninput" @bind-value="titleSearch" />
    
    
    
    @code {
    
        private string titleSearch{ get; set; }
    }
    

    现在,在每次敲击键盘时都会调用输入事件,并且 titleSearch 属性的值会更新以反映这一点...

    希望对您有所帮助...

    【讨论】:

      【解决方案2】:

      和Isaac类似,也可以这样,用3.1预览版测试过

      <input type="text" @bind:event="oninput" @bind="titleSearch" />
      
      <label>@titleSearch</label>
      @code { public string titleSearch { get; set; }}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-07
        • 1970-01-01
        • 2013-05-28
        • 2011-04-14
        • 1970-01-01
        相关资源
        最近更新 更多