Knockout 和 jQuery Mobile:复选框

发布于 2024-12-11 02:31:08 字数 354 浏览 0 评论 0原文

我正在尝试将复选框和标签元素动态添加到文档中。 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 技术交流群。

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

发布评论

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

评论(5

时光暖心i 2024-12-18 02:31:08

我也遇到过这个问题。不幸的是,这里的所有建议要么对我不起作用,要么有其他问题。因此,我创建了一个简单的自定义绑定,适用于所有版本的 KO(包括最新的 v3):

ko.bindingHandlers.jqmChecked = {
    init: ko.bindingHandlers.checked.init,
    update: function (element, valueAccessor) {
        //KO v3 and previous versions of KO handle this differently
        //KO v3 does not use 'update' for 'checked' binding
        if (ko.bindingHandlers.checked.update) 
            ko.bindingHandlers.checked.update.apply(this, arguments); //for KO < v3, delegate the call
        else 
            ko.utils.unwrapObservable(valueAccessor()); //for KO v3, force a subscription to get further updates

        if ($(element).data("mobile-checkboxradio")) //calling 'refresh' only if already enhanced by JQM
            $(element).checkboxradio('refresh');
    }
};

应该像这样使用:

<input type="checkbox" data-bind="jqmChecked: someValue" id="checkbox1"/>

在此处查看完整的工作示例:

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):

ko.bindingHandlers.jqmChecked = {
    init: ko.bindingHandlers.checked.init,
    update: function (element, valueAccessor) {
        //KO v3 and previous versions of KO handle this differently
        //KO v3 does not use 'update' for 'checked' binding
        if (ko.bindingHandlers.checked.update) 
            ko.bindingHandlers.checked.update.apply(this, arguments); //for KO < v3, delegate the call
        else 
            ko.utils.unwrapObservable(valueAccessor()); //for KO v3, force a subscription to get further updates

        if ($(element).data("mobile-checkboxradio")) //calling 'refresh' only if already enhanced by JQM
            $(element).checkboxradio('refresh');
    }
};

Should be used like this:

<input type="checkbox" data-bind="jqmChecked: someValue" id="checkbox1"/>

See a complete working example here:

http://jsfiddle.net/srgstm/ub6sq/

千仐 2024-12-18 02:31:08

请参阅: https://gist.github.com/1006808

然后你可以执行如下操作:

var $checkbox = $('input[type="checkbox"]');
$checkbox.checkboxradio();
$checkbox.dataBind({
    your options..
});

希望这会有帮助的!

See: https://gist.github.com/1006808

Then you can do something like the following:

var $checkbox = $('input[type="checkbox"]');
$checkbox.checkboxradio();
$checkbox.dataBind({
    your options..
});

Hope this'll help!

感性不性感 2024-12-18 02:31:08

使用剔除默认检查绑定与样式对象(如 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.

享受孤独 2024-12-18 02:31:08

我创建了一个与 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 a div.

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.

蒲公英的约定 2024-12-18 02:31:08

以下是我为 jQueryMobile 复选框构建的自定义处理程序中经过大量注释的代码:

ko.bindingHandlers.checkbox = {
init: function(element, valueAccessor) {

    // set the dom element to a checkbox and initialize it (for jquerymobile)
    var checkbox = $(element);
    checkbox.checkboxradio();
    checkbox.attr('type', 'checkbox');

    // register change event to update the model on changes to the dom
    ko.utils.registerEventHandler(element, "change", function() {
        valueAccessor()(

            // off because it is before the ui has refreshed
            $(this).siblings('label.ui-checkbox-off').length > 0
        );
    });
},
update: function(element, valueAccessor) {

    // update the checked binding, i.e., check or uncheck the checkbox
    ko.bindingHandlers.checked.update(element, valueAccessor)

    // and refresh the element (for jquerymobile)
    var checkbox = $(element);
    checkbox.checkboxradio('refresh')
}
};

Here is my heavily commented code on a custom handler I built for jQueryMobile checkboxes:

ko.bindingHandlers.checkbox = {
init: function(element, valueAccessor) {

    // set the dom element to a checkbox and initialize it (for jquerymobile)
    var checkbox = $(element);
    checkbox.checkboxradio();
    checkbox.attr('type', 'checkbox');

    // register change event to update the model on changes to the dom
    ko.utils.registerEventHandler(element, "change", function() {
        valueAccessor()(

            // off because it is before the ui has refreshed
            $(this).siblings('label.ui-checkbox-off').length > 0
        );
    });
},
update: function(element, valueAccessor) {

    // update the checked binding, i.e., check or uncheck the checkbox
    ko.bindingHandlers.checked.update(element, valueAccessor)

    // and refresh the element (for jquerymobile)
    var checkbox = $(element);
    checkbox.checkboxradio('refresh')
}
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文