【发布时间】:2016-09-01 15:49:51
【问题描述】:
我有一个可观察的淘汰赛:self.productBarcode = ko.observable(),我使用 jquery 自动完成功能在产品列表中搜索,如果找到产品,我在其中有一个选择事件将所选对象添加到可观察对象的自动完成功能:
select: function (event, ui) {
updateElementValueWithLabel(event, ui);
self.productBarcode(ui);
ui 对象的格式如下:
ui
{
item
{
barcode: "2"
label: "p1"
value: "p1"
}
}
那么我需要从productBarcode中选择与ui格式相同的产品barcode。
问题:如何从可观察的 productBarcode 访问条形码属性? 我尝试了以下方法:
self.addNewSale = function() {
var placeNewSale = {
StoreId: self.SaleStoreObject().Id,
StoreName: self.SaleStoreObject().Name,
ProductBarcode: self.productBarcode().barcode,
ProductName: self.productBarcode().label,
Quantity: self.newSale.Quantity()
}
self.placeSaleProducts().push(placeNewSale);
self.placeSaleProducts(self.placeSaleProducts());
}
【问题讨论】:
-
我不完全清楚你在问什么......预期的行为是什么,你当前代码的结果是什么? (另外,请注意,您可以通过在
.push之前省略()直接推送到observableArray) -
我正在尝试从具有 ui 格式的对象访问条形码属性
-
喜欢
obj.item.barcode? -
我无法直接访问类似:self.productBarcode().barcode,因为我知道这是 undefined
-
是的,像 obj.item.barcode 之类的东西
标签: javascript jquery knockout.js