我已经创建了模板.html
<div data-bind="foreach: productOverGift, visible: expanded" class="free-gift-oder75">
<div class="product-over-info">
<input type="radio" class="product-information" name="productId" data-bind="attr: { value: id, id:'product_over_'+id}, click: $parent.selectItem(id, sku)"/>
<image data-bind="attr: { src: image}" class="product-image" width="120" height="150" alt="Product Image" style="width: 50px; height: 50px"/>
<span data-bind="text: name"></span>
</div>
</div>
并创建文件js
define(
[
'jquery',
'ko',
'uiComponent',
'underscore',
'Magento_Checkout/js/model/step-navigator',
'Magento_Customer/js/model/customer',
'Magento_Checkout/js/model/quote',
'mage/url',
'mage/storage',
'Magento_Checkout/js/action/get-totals',
'mage/translate',
'Magento_Ui/js/model/messageList',
'Magento_Checkout/js/model/full-screen-loader'
],
function (
$,
ko,
Component,
_,
stepNavigator,
customer,
quote,
urlBuilder,
storage,
getTotal,
$t,
messageList,
startLoader
) {
'use strict';
/**
* check-login - is the name of the component's .html template
*/
return Component.extend({
defaults: {
template: 'Custom_Checkout/promotion'
},
//add here your logic to display step,
isVisible: ko.observable(true),
isLogedIn: customer.isLoggedIn(),
//step code will be used as step content id in the component template
stepCode: 'promotion-step',
//step title value
stepTitle: 'Logging Status',
productOverGift: ko.observableArray([]),
freeGiftWithPurchase: ko.observableArray([]),
productImageSelected: ko.observable(""),
productNameSelected: ko.observable(""),
idProductSelected: ko.observable(""),
quoteId: ko.observable(""),
isChecked: ko.observable(true),
skuProductSelected: ko.observable(""),
messageError: ko.observable(""),
message: ko.observable(""),
expanded: ko.observable(true),
arrowClass: ko.observable('fa fa-angle-double-up'),
arrowTitle: ko.observable($t('Shrink')),
dataShippingPnr: ko.observable(""),
dataShippingFullName: ko.observable(""),
dataShippingEmail: ko.observable(""),
dataSegmentDisplay: ko.observable(""),
isCheckedOver: ko.observable(true),
isDeleted: false,
/**
*
* @returns {*}
*/
initialize: function () {
this._super();
//register your step
stepNavigator.registerStep(
this.stepCode,
//step alias
null,
this.stepTitle,
//observable property with logic when display step or hide step
this.isVisible,
_.bind(this.navigate, this),
/**
* sort order value
* 'sort order value' < 10: step displays before shipping step;
* 10 < 'sort order value' < 20 : step displays between shipping and payment step
* 'sort order value' > 20 : step displays after payment step
*/
15
);
return this;
},
/**
* select radio
* @param id
* @param quoteId
* @param sku
*/
selectItem: function (id, sku) {
var self = this;
var listProduct = this.productOverGift();
var itemChecked = $('#product_over_' + id).is(":checked");
if (itemChecked) {
if (listProduct.length > 0) {
listProduct.forEach(function (item, i) {
if (item.id === id) {
self.productImageSelected(item.image);
self.productNameSelected(item.name);
self.idProductSelected(item.id);
self.skuProductSelected(item.sku);
}
});
}
// self.deleteItemInCart(oldProductSku, this.quoteId());
// self.saveProductToOrder(id, this.quoteId(), sku);
}
},
});
}
);
我有一个函数“selectItem()”在input =〉标签的循环中使用,在单击(选中)后将调用“selectItem“函数。但现在“selectItem“函数总是在页面加载时调用,这不是我想要的。我只是希望它在单击input后被调用。请帮助我。谢谢
1条答案
按热度按时间rsaldnfx1#
此时,您正在HTML中执行函数,而不是将对它的引用绑定到click处理程序。这将在加载页面时执行该函数:
相反,您应该将对函数的引用绑定到单击处理函数:
访问
id
和sku
属性的方法有很多种,但最简单的方法可能是意识到传递给click处理程序的第一个参数是forEach: productOverGift
中的当前模型。因此,将selectItem
函数的签名重写为如下所示:这应该可以让它为你工作。然而,你可能会考虑看看
checked
绑定。这可以帮助你整理你的代码,以一种更像KO的风格来完成它,但这不是必需的。如果你有兴趣解决这个问题,那么你可以直接把radio的值绑定到itemChecked
属性,并使用一个观察器来运行你的selectItem代码:或者,
productImage
选择属性可以是从itemChecked
导出的计算值。这将消除代码中的jQuery依赖性,并开始以一种更被动的KO风格来完成这一切,但要做到这一点,您需要将一些代码从
$parent
中移到产品VM中。