如何强制 observable 从 DOM 元素读取值
我有一个隐藏输入并将值绑定到模型的可观察属性。我正在使用另一个插件,它在内部更新隐藏输入的值,但是可观察的值不会更新它的值。
我如何强制可观察者从元素更新它的值?
像 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道你已经想出了解决方案。
但我想我应该发布自己的内容,以防有人遇到这个问题,因为它在谷歌结果中排名靠前,没有答案,所以可能对某人有用。
和 html
因为隐藏的输入通常没有更改事件,所以当您更改值时,您需要触发自己的事件,例如
我不确定这是否是最好的解决方案,但我可以提供最好的解决方案我正在努力的时间范围:-) 如果我想出更优雅的东西,我可能会将一些东西放入淘汰赛维基中。
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.
And the html
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.
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.
它通过为我的案例添加
$("hiddenInputId").trigger("change");
来工作,而不添加 ko 绑定处理程序。谢谢 !
It worked by adding
$("hiddenInputId").trigger("change");
for my case without adding a ko binding handlers.Thanks !