Knockout 和 jQuery Mobile:复选框
我正在尝试将复选框和标签元素动态添加到文档中。 Checkbox 元素具有 Knockout 的 data-bind 属性,可将其值绑定到 ViewModel 中的可观察值。但是,当我尝试通过执行
$('input[type="checkbox"]').checkboxradio();
数据绑定属性来使用 jQuery Mobile 设置复选框样式时,该属性将被删除。如果我省略上面的行,数据绑定属性将被正确设置并且绑定有效。
有没有办法同时拥有 jQuery Mobile 样式和 Knockout 绑定?
我正在使用 jQuery Mobile RC1 和 Knockout 1.2.1。
I'm trying to dynamically add checkbox and label elements to the document. Checkbox element has Knockout's data-bind attribute to bind its value to an observable value in the ViewModel. However when I try to style the checkboxes with jQuery Mobile by executing
$('input[type="checkbox"]').checkboxradio();
data-bind attributes will be removed. If I leave out the above line, data-bind attributes are properly set and the binding works.
Is there a way to have both jQuery Mobile styling and Knockout bindings at the same time?
I'm using jQuery Mobile RC1 and Knockout 1.2.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我也遇到过这个问题。不幸的是,这里的所有建议要么对我不起作用,要么有其他问题。因此,我创建了一个简单的自定义绑定,适用于所有版本的 KO(包括最新的 v3):
应该像这样使用:
在此处查看完整的工作示例:
http://jsfiddle.net/srgstm/ub6sq/
I have also encountered this problem. Unfortunately, all the suggestions here either did not work for me or had other issues. So I have created a simple custom binding that works in all versions of KO (including the latest v3):
Should be used like this:
See a complete working example here:
http://jsfiddle.net/srgstm/ub6sq/
请参阅: https://gist.github.com/1006808
然后你可以执行如下操作:
希望这会有帮助的!
See: https://gist.github.com/1006808
Then you can do something like the following:
Hope this'll help!
使用剔除默认检查绑定与样式对象(如 jQuery mobile 那样)存在问题。它与 jQueryUi 的 Button/Buttonset 功能具有相同的问题。复选框上方有一个标签,指示正在发生的情况,并且它无法通过标准剔除检查绑定正确更新。
在 http://therunningprogrammer 进行了讨论.blogspot.com/2011/10/how-to-use-jquery-uis-button-with.html。
要直接对 jQuery Mobile 中的这些样式对象使用淘汰赛,必须修改演示的代码以处理不同的 DOM 上下文。当我有空闲时间时,我会发布代码更新。
编辑
在 Google 网上论坛 - Knockout 中 , luv2hike 发布了一个解决方案。您可以在 http://jsfiddle.net/luv2hike/nrJBC/ 上看到它的工作情况。看起来可以解决您的问题。
There is a problem with using knockouts default checked binding with styled objects like jQuery mobile does. It has the same issues that jQueryUi's Button/Buttonset functions. There is a label over the checkbox that indicates what is happening and it doesn't get updated properly via standard knockout checked binding.
It is discussed at http://therunningprogrammer.blogspot.com/2011/10/how-to-use-jquery-uis-button-with.html.
To use knockout directly with these styled objects from jQuery Mobile, the demonstrated code will have to be modified to handle the different DOM context. I'll post an update to the code when I can get some free time to do it.
EDIT
In Google Groups - Knockout, luv2hike posted a solution. You can see it working at http://jsfiddle.net/luv2hike/nrJBC/. Looks like a working fix for your problem.
我创建了一个与 jQuery Mobile 1.2.0 和 Knockout 2.2.1 配合使用的简单绑定,并与默认的 jQuery Mobile 复选框配合使用。此绑定不依赖于自定义图标或 JQuery Mobile 的 CSS 样式。它还允许在 HTML 中使用常规复选框标记 (
),而不是使用
div< 等替代标记元素/代码>。
这是小提琴: http://jsfiddle.net/thedude458/52baX/
注意:< /strong> 目前,该示例仅支持单个复选框,而不支持列表,因为这就是我当前所需要的。它还假设绑定属性是可观察的。
I created a simple binding that works with jQuery Mobile 1.2.0 and Knockout 2.2.1 and works with default jQuery mobile checkboxes. This binding has no dependency on custom icons or JQuery Mobile's CSS styles. It also allows the use of regular checkbox markup in your HTML (
<input type="checkbox" ... />
) as opposed to using an alternate markup element like adiv
.Here's the fiddle: http://jsfiddle.net/thedude458/52baX/
Note: Presently, the example only supports a single checkbox, not a list, as that is all I currently have a need for. It also assumes that the bound property is an observable.
以下是我为 jQueryMobile 复选框构建的自定义处理程序中经过大量注释的代码:
Here is my heavily commented code on a custom handler I built for jQueryMobile checkboxes: