javascript - KnockoutJS 新添加的元素没有样式表中的任何样式

标签 javascript jquery css jquery-mobile knockout.js

我正在尝试使用类似于此页面的 KnockOut:http://knockoutjs.com/examples/cartEditor.html

但是,当与 JQuery Mobile 主题一起使用时 - 当您从类别下拉列表中选择一个元素时,它会在产品标题下添加一个下拉列表 - 但是刚刚添加的第二个下拉列表没有移动样式应用。

我在这里添加了一个 fiddle :http://jsfiddle.net/mtait/adNuR/1927/ - 展示。 我可以添加什么来将样式添加到新的下拉列表中?

function formatCurrency(value) {
return "$" + value.toFixed(2);
}

var CartLine = function() {
var self = this;
self.category = ko.observable();
self.product = ko.observable();
self.quantity = ko.observable(1);
self.subtotal = ko.computed(function() {
    return self.product() ? self.product().price * parseInt("0" + self.quantity(), 10) : 0;
});

// Whenever the category changes, reset the product selection
self.category.subscribe(function() {
    self.product(undefined);
});
};

var Cart = function() {
// Stores an array of lines, and from these, can work out the grandTotal
var self = this;
self.lines = ko.observableArray([new CartLine()]); // Put one line in by default
self.grandTotal = ko.computed(function() {
    var total = 0;
    $.each(self.lines(), function() { total += this.subtotal() })
    return total;
});

// Operations
self.addLine = function() { self.lines.push(new CartLine()) };
self.removeLine = function(line) { self.lines.remove(line) };
self.save = function() {
    var dataToSave = $.map(self.lines(), function(line) {
        return line.product() ? {
            productName: line.product().name,
            quantity: line.quantity()
        } : undefined
    });
    alert("Could now send this to server: " + JSON.stringify(dataToSave));
};
};

ko.applyBindings(new Cart());

谢谢,

标记

最佳答案

我认为您需要一个客户绑定(bind)处理程序。

处理程序:

ko.bindingHandlers.jqmOptions = {
    update: function (element, valueAccessor, allBindingsAccessor, context) {
        ko.bindingHandlers.options.update(element, valueAccessor, allBindingsAccessor, context);
        $(element).selectmenu();
        $(element).selectmenu("refresh", true);
    }
};

绑定(bind):

<select data-bind='jqmOptions : sampleProductCategories, optionsText: "name", optionsCaption: "Select...", value: category'> </select>

fiddle :http://jsfiddle.net/adNuR/1942/

关于javascript - KnockoutJS 新添加的元素没有样式表中的任何样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18442745/

相关文章:

javascript - &lt;script&gt; 标签上的 data-main 属性不加载 require.js 有效

javascript - jQuery 选择最近元素的子元素

jQuery 文本编辑器不起作用?

javascript - 如何使用 Jquery addClass( ) 添加和删除选择输入的类?

javascript - 如何使div内容向右溢出时隐藏

javascript - 交叉处理算法(图像处理)

javascript - 同源策略仅适用于 XMLHttpRequest?

javascript - 如何用备用模拟服务器替换 $httpBackend?

css - 垂直自动时如何使用CSS保持水平可见?

javascript - Tablesorter 斑马在排序之前不会条纹