使用 Jquery 更改值时 KnockoutJS 属性不会更新
我有一系列复选框,我想收集选定的一个。复选框位于 div 中,当单击 div 时,复选框也应该被选中:
var oCheckBox = $($(this).find('.chkSocialMediaItem').get(0));
oCheckBox.attr('checked', !$(oCheckBox).attr('checked'));
这工作得很好,但 KnockoutJS 不会接受更改,因此不会更新所选项目的计数器。
我在某处读到您需要触发更改事件。但是当我监听复选框上的更改事件时,它实际上会被触发。
任何帮助将不胜感激, 谢谢 !
更新:
我找到了“淘汰”解决方案。 在我的 div 中,我对“单击”进行了数据绑定,并更改了该函数中的检查值:
<script type="text/html" id="Template">
<div class="item" data-bind="click: DivClicked">
<input type="checkbox" data-bind="checked: IsASelectedItem" class="chkSocialMediaItem"/>
</div>
</script>
function SocialMediaSearchItem() {
this.IsASelectedItem = ko.observable();
this.DivClicked = function () {
this.IsASelectedItem(!this.IsASelectedItem());
};
}
这是唯一的解决方案吗?我真的很希望 2 也能看到一个 Jquery 解决方案。
I have a serie of checkboxes and I want to gather the selected one. The checkboxes are in div's and when the div is clicked the checkbox should get checked as well:
var oCheckBox = $($(this).find('.chkSocialMediaItem').get(0));
oCheckBox.attr('checked', !$(oCheckBox).attr('checked'));
This works just fine but KnockoutJS doesn't pick up the change and so doesn't update my counter on the selected items.
I read somewhere you need to trigger the change event. But when I listen to the change event on the checkbox it does actually get triggered.
Any help would be appreciated,
Thanks !
Update:
I have found a 'knockout' solution.
In my div I did a data-bind off the 'click' and changed the checked value in that function:
<script type="text/html" id="Template">
<div class="item" data-bind="click: DivClicked">
<input type="checkbox" data-bind="checked: IsASelectedItem" class="chkSocialMediaItem"/>
</div>
</script>
function SocialMediaSearchItem() {
this.IsASelectedItem = ko.observable();
this.DivClicked = function () {
this.IsASelectedItem(!this.IsASelectedItem());
};
}
Is this the only solution? I would really like 2 see a Jquery solution as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题......虽然我确实找到了一个适合我的解决方案,但与 @RP Niemeyer 发布的解决方案不同,所以我想我会发布它。
Knockout 版本:1.2.1 -- (knockout-1.2.1.js)
要取消选中 Knockout 绑定的复选框,我使用以下命令
来检查 Knockout 绑定的复选框我使用了以下命令
希望如果其他解决方案不起作用,也许这会=) ...在这个问题上浪费了大约3个小时。
I ran into the same issue... Although I did find a solution that worked for me that differed from the one posted by @RP Niemeyer, so I figured I would post it.
Knockout Version: 1.2.1 -- (knockout-1.2.1.js)
To uncheck the checkbox bound by Knockout I used the following
to check the checkbox bound by Knockout I used the following
Hopefully if the other solution didnt work, perhaps this will =) ... wasted about 3 hours on this issue.
checked
绑定仅侦听click
事件。根据设置实际选中值的时间,手动触发单击事件最终会使框不同步。设置该值的首选方法是更新视图模型值,正如您在上面的第二个示例中所做的那样。
但是,如果您使用 1.3beta,那么是使用 ko.dataFor API 连接元素与其相应数据的简单方法。它看起来像:
The
checked
binding only listens to theclick
event. Manually triggering the click event ends up getting the box out-of-sync based on when the actual checked value is set.The preferred way to set the value is to update your view model value, as you have done in your second example above.
However, if you are using 1.3beta, then there is an easy way to connect an element with its corresponding data using the
ko.dataFor
API. It would look something like: