2014-02-22 34 views
0

http://jsfiddle.net/descending/adNuR/2523/淘汰赛车的示例中,下拉选择信息

因此,这是淘汰赛的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); 
    }); 
}; 

回答

1

的方法添加到CartLine得到信息:

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

将其绑定到click事件...

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

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

+0

谢谢这个结合

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

工作的例子!这是完美的 – Descending

0

制作新方法

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

和HTML加上http://jsfiddle.net/adNuR/2525/

+0

谢谢!这也是完美的! – Descending