简体   繁体   English

绑定到 select 元素时出现 Blazor 问题

[英]Blazor issue when binding to a select element

I am using @bind-value to bind the selected value of a select element to a variable in Blazor. It works fine, if I physically click the drop down and select a value.我正在使用@bind-value将 select 元素的选定值绑定到 Blazor 中的一个变量。如果我物理单击下拉列表和 select 一个值,它工作正常。 However, if I select a value in the drop down through Javascript, Blazor doesn't seem to detect the change.但是,如果我 select 通过 Javascript 下拉一个值,Blazor 似乎没有检测到变化。 I wrote a very simple program to demonstrate.我写了一个非常简单的程序来演示。

Here is my component .cshtml code:这是我的组件.cshtml代码:

@page "/"
@inject IJSRuntime JsRuntime

<select id="my-select-box" @bind-value="MyValue" @bind-value:event="onchange">
    <option value="0">-- Select Something --</option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
</select>

<br />

@if (MyValue == "0") {
    <div>Select an option</div>
} else {
    <div>You chose @MyValue</div>
}

<div style="margin-top: 20px">
    <button @onclick="@(() => SelectOptionThroughJs("1"))">Select Option 1</button>
    <button @onclick="@(() => SelectOptionThroughJs("2"))">Select Option 2</button>
    <button @onclick="@(() => SelectOptionThroughJs("3"))">Select Option 3</button>
</div>

@code {
    public string MyValue {get;set;} = "0";

    public async Task SelectOptionThroughJs(string option)
    {
        await JsRuntime.InvokeVoidAsync("helperFunctions.selectOption", option);
    }
}

And here is my javascript code:这是我的 javascript 代码:

window.helperFunctions = {
    selectOption: function(op) {
        var selectBoxElement = document.getElementById("my-select-box");

        selectBoxElement.value = op;
    }
}

I also created a fiddle to demonstrate .我还创建了一个小提琴来演示

If you physically select an item from the drop down, you'll see it works just fine.如果您从下拉列表中实际 select 一个项目,您会发现它工作得很好。 However, if you click one of the 3 buttons, you'll see the value does change in the drop down, but Blazor does not seem to realize the value changed.但是,如果您单击 3 个按钮之一,您会看到下拉列表中的值确实发生了变化,但 Blazor 似乎没有意识到该值发生了变化。

How can I fix this issue?我该如何解决这个问题?

Blazor does not seem to realize the value changed Blazor 好像没意识到值变了

Exactly... Your select element is bound to the property MyValue, and whenever the current component is rendered, the select element gets its value from the property.确切地说...您的 select 元素绑定到属性 MyValue,并且无论何时呈现当前组件,select 元素都从该属性获取其值。 Changing the selection through JS Interop has no effect on the property MyValue.通过 JS Interop 更改选择对属性 MyValue 没有影响。 The easiest was to solve this issue is to set the value of MyValue to the parameter you pass to the JS function, like this:解决这个问题最简单的方法是将 MyValue 的值设置为您传递给 JS function 的参数,如下所示:

MyValue  = option;

but then calling the JS function becomes superfluous... Perhaps, you can tell us what you're trying to achieve.但随后调用 JS function 变得多余......也许,你可以告诉我们你想要实现的目标。

A point to remember, you shouldn't use JS for such tasks that interfere with the rendering system of Blazor.需要记住的一点是,您不应该将 JS 用于此类会干扰 Blazor 渲染系统的任务。

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

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