【问题标题】:Knockout Cart example, Drop Down selected info淘汰车示例,下拉选择的信息
【发布时间】:2014-03-23 22:06:21
【问题描述】:

http://jsfiddle.net/descending/adNuR/2523/

这是 Knockout Js 汽车编辑器示例,我添加了一个东西,即获取信息链接。当用户单击获取信息时,我需要能够添加项目并获取该行的选定项目。

我到底如何才能做到这一点?我被难住了

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);
    });
};

【问题讨论】:

    标签: javascript jquery knockout.js


    【解决方案1】:

    向CartLine添加方法以获取信息:

    self.getInfo = function () {
        var category = self.category() ? self.category().name : "";
        var product = self.product() ? self.product().name : "";
        alert(category + ": " + product + ": " + self.quantity());
    }
    

    将其绑定到点击事件...

    <a href='#' data-bind='click: getInfo'>Get Info</a>
    

    小提琴:http://jsfiddle.net/adNuR/2524/

    【讨论】:

      【解决方案2】:

      制作新方法

      self.getInfo=function(d){
         alert("Category is "+d.category().name+" and product is"+d.product().name+" and quantity is "+d.quantity());
      }
      

      并在 html 中添加此绑定

      <a href='#' data-bind='click:$parent.getInfo,visible: product'>Get Info</a>
      

      http://jsfiddle.net/adNuR/2525/ 上的工作示例

      【讨论】:

      • 谢谢!这也是完美的!
      猜你喜欢
      • 2015-11-05
      • 1970-01-01
      • 2015-05-06
      • 2014-06-26
      • 2023-03-08
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多