如何强制 observable 从 DOM 元素读取值

发布于 2024-12-01 02:46:13 字数 148 浏览 0 评论 0原文

我有一个隐藏输入并将值绑定到模型的可观察属性。我正在使用另一个插件,它在内部更新隐藏输入的值,但是可观察的值不会更新它的值。

我如何强制可观察者从元素更新它的值?

像 valueHasMutated 之类的东西,但以另一种方式。

谢谢

I have a hidden input and bind the value to an observable property of my model. I'm using another plugin that internally updates the value of that hidden input, however the observable doesn't update it's value.

How can i force the observable to update it's value from the element?

something like valueHasMutated but in the other way.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天邊彩虹 2024-12-08 02:46:13

我知道你已经想出了解决方案。

但我想我应该发布自己的内容,以防有人遇到这个问题,因为它在谷歌结果中排名靠前,没有答案,所以可能对某人有用。

 ko.bindingHandlers.hiddenInputValue = {

        init: function (element, valueAccessor) {

            $(element).bind("change", function (event, data, formatted) { //hidden vars don't usually have change events, so we trigger $myElement.trigger("change");
                var value = valueAccessor();
                value($(this).val()); //rather than $(this).val(), might be best to pass our custom info in data
            });
        },
        update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
            var value = valueAccessor();
            $(element).val(value);
        }

    };

和 html

<input type="hidden" name="myName"  id="myId"  data-bind="hiddenInputValue: myModelValue" >

因为隐藏的输入通常没有更改事件,所以当您更改值时,您需要触发自己的事件,例如

$("#myId").trigger("change");

我不确定这是否是最好的解决方案,但我可以提供最好的解决方案我正在努力的时间范围:-) 如果我想出更优雅的东西,我可能会将一些东西放入淘汰赛维基中。

I realise you've already come up with a solution.

But I'd thought I'd post my own incase anyone comes across this, as it's up high in the Google results with no answers, so might come in useful for someone.

 ko.bindingHandlers.hiddenInputValue = {

        init: function (element, valueAccessor) {

            $(element).bind("change", function (event, data, formatted) { //hidden vars don't usually have change events, so we trigger $myElement.trigger("change");
                var value = valueAccessor();
                value($(this).val()); //rather than $(this).val(), might be best to pass our custom info in data
            });
        },
        update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
            var value = valueAccessor();
            $(element).val(value);
        }

    };

And the html

<input type="hidden" name="myName"  id="myId"  data-bind="hiddenInputValue: myModelValue" >

Because hidden inputs don't have change events normally, you'll need to trigger your own event when ever you change the value e.g.

$("#myId").trigger("change");

I'm not sure if this is the best solution, but the best I could come with in the timescale I'm working to :-) I might put something into the knockout wiki if I come up with something more elegant.

素年丶 2024-12-08 02:46:13

它通过为我的案例添加 $("hiddenInputId").trigger("change"); 来工作,而不添加 ko 绑定处理程序。

谢谢 !

It worked by adding $("hiddenInputId").trigger("change"); for my case without adding a ko binding handlers.

Thanks !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文