我有三个这样的无线电按钮:
<input type="radio" name="IsPerCentOrDollar" value="1" data-bind="checked: IsPerCentOrDollar" />
<span>Percentage Amount</span>
<input type="radio" name="IsPerCentOrDollar" value="2" data-bind="checked: IsPerCentOrDollar" />
<span>Dollar Amount</span>
@Html.TextBoxFor(p => p.DiscountValue, new { @class = "form-control", @data_bind = "value: DiscountValue, valueUpdate: 'afterkeydown', disable: IsPerCentOrDollar() == '3'", @maxlength = "50" })
<input type="radio" value="3" class="freeShipping" name="free" data-bind="checked: IsPerCentOrDollar" />
<label>Free Shipping</label>
IsPerCentOrDollar用于在选择免费送货时禁用文本框。
self.IsPerCentOrDollar = ko.observable();
viewmodel:
function CouponViewModel(data) {
selfCoupon = this;
self.IsPerCentOrDollar = ko.observable();
selfCoupon.discountType = ko.observable();
selfCoupon.Code = ko.observable(data.Code);
selfCoupon.DiscountValue = ko.observable(data.DiscountValue);
selfCoupon.DiscountLevel = ko.observable("1");
};
我已将DiscountLevel的默认值设置为1。 无论点击哪个单选按钮,都会保留此信息。
所以我想要的是点击radiobutton的值进入DiscountLevel。
任何线索,建议?
答案 0 :(得分:0)
尝试这样:
self.IsPerCentOrDollar.subscribe(function(newValue) {
selfCoupon.DiscountLevel(newValue);
});
修改强>
function CouponViewModel(data) {
selfCoupon = this;
selfCoupon.IsPerCentOrDollar = ko.observable();
selfCoupon.discountType = ko.observable();
selfCoupon.Code = ko.observable(data.Code);
selfCoupon.DiscountValue = ko.observable(data.DiscountValue);
selfCoupon.DiscountLevel = ko.observable("1");
selfCoupon.IsPerCentOrDollar.subscribe(function(newValue) {
selfCoupon.DiscountLevel(newValue);
});
};
答案 1 :(得分:0)
在你的情况下,我认为它可能看起来像:
function CouponViewModel(data) {
selfCoupon = this;
self.IsPerCentOrDollar = ko.observable("1");
selfCoupon.discountType = ko.observable();
selfCoupon.Code = ko.observable(data.Code);
selfCoupon.DiscountValue = ko.observable(data.DiscountValue);
selfCoupon.DiscountLevel = ko.computed(function () {
return self.IsPerCentOrDollar();
});
};
如果你还想在开始时检查第一个单选按钮(对我来说很合乎逻辑)或只是:
self.IsPerCentOrDollar.subscribe(function(newValue) {
selfCoupon.DiscountLevel(newValue);
});
如果你不想这样做。
每次IsPerCentOrDollar更改时,这将为IsPerCentOrDollar的值设置DiscountLevel。