简体   繁体   English

无法获取由 Javascript 设置的输入值,然后由 Blazor 按钮单击读取

[英]Cannot get input value set by Javascript and then read by Blazor button click

Inside a .NET 5 blazor application I use a javascript library for barcode reading.在 .NET 5 blazor 应用程序中,我使用 javascript 库来读取条形码。 When I press a button on blazor page I call javascript function.当我按下 blazor 页面上的按钮时,我调用 javascript function。 Javascript function reads the barcode and sets value of an input field. Javascript function 读取条形码并设置输入字段的值。

There is an input field bound to string value:有一个绑定到字符串值的输入字段:

<input id="result" @bind="@Received"/>

@code { private string Received { get; set; } }

A button click calls javascript function:单击按钮调用 javascript function:

public async Task Start()
{
    await JSRuntime.InvokeVoidAsync("startBarcodeReader");
}

Javascript function sets value of input field: Javascript function 设置输入字段的值:

document.getElementById('result').value = "Barcode Value";

Everything works as I expected and I see barcode value on my screen.一切都按我的预期工作,我在屏幕上看到条形码值。 I would like to use Received when I click another button.当我单击另一个按钮时,我想使用Received However bound value is null.但是绑定值为 null。 It is not set.未设置。

public async Task Add()
{
    // Received is null
    await Task.CompletedTask;
}

If I manually type something into input field then value of Received variable is set.如果我在输入字段中手动输入内容,则设置 Received 变量的值。 It seems that binding is not set bound variable unless focus is lost.除非失去焦点,否则似乎没有设置绑定变量。

How can I get the value I see in input field?如何获取我在输入字段中看到的值? I tried to use oninout event of inout field and did not work.我尝试使用 inout 字段的 oninout 事件,但没有成功。

As a rule, your JS code should not interfere in any way with the rendering and bounding features of Blazor.通常,您的 JS 代码不应以任何方式干扰 Blazor 的渲染和边界功能。 Thus, you cannot assign a value to an input field in JavaScript and expect this value to pass to the bounding variable Received .因此,您不能将值分配给 JavaScript 中的输入字段并期望该值传递给边界变量Received This can never occur.这永远不会发生。 How can Blazor know that the value of the input field has changed? Blazor如何知道输入字段的值发生了变化? Is there any event listener attached to the input field that is to notify Blazor that a value has been entered.是否有任何事件侦听器附加到输入字段以通知 Blazor 已输入值。 None.没有任何。

Solution: Pass the value to a Blazor method in which you can update the variable Received ... After that the component will re-render, and the input field will show the same value inside it, but this time it is done by the rendering and bounding features of Blazor.解决方案:将值传递给 Blazor 方法,您可以在该方法中更新变量Received ... 之后组件将重新渲染,输入字段将在其中显示相同的值,但这次是通过渲染完成的和 Blazor 的边界特征。

Note: You can employ DotNetObjectReference to call your JavaScript, passing it a reference to a C# object that defines a method to receive the value of the barcode when called from JavaScript. Note: You can employ DotNetObjectReference to call your JavaScript, passing it a reference to a C# object that defines a method to receive the value of the barcode when called from JavaScript.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM