[英]How to update my tax based on user selection
I'm trying to figure out how to update my tax percentage when user selects a particular state.我试图弄清楚当用户选择特定州时如何更新我的税收百分比。 Here's my code (stripped of unrelated data):
这是我的代码(去除了不相关的数据):
<select data-bind="event: {change: checkTax }">
<option value="PA">PA</option>
<option value="FL">FL</option>
</select>
<span data-bind="text: taxedItems"></span>
var MainViewModel = function() {
var self = this;
var response = arrayFromJson;
// ...
self.checkTax = function (viewModel, event) {
if (event.target.value === "FL") {
userTax = "this should appear";
}
};
self.taxedItems = ko.pureComputed(function() {
taxcost = userTax;
return taxcost;
});
self.CartItems($.map(response, function(item) {
return new AppViewModel(item);
}));
};
function AppViewModel(data) {
var self = this;
// ...
}
ko.applyBindings(new MainViewModel());
At first taxedItems
should be null, then after the user changes it to FL
, taxedItems
should say "this should appear"
but it doesn't it just bypasses updating taxedItems
altogether.起初
taxedItems
应该是空的,然后在用户将其更改为FL
, taxedItems
应该说"this should appear"
但它不仅仅是完全绕过更新taxedItems
。 Does anyone know why this is?有人知道为什么是这样吗?
if you want to have taxedItems in your mainViewModel you can do this :如果你想在你的 mainViewModel 中有 taxedItems 你可以这样做:
VM :虚拟机:
var MainViewModel = function () {
var self = this;
self.CartItems = ko.observableArray([]);
self.States = ko.observableArray([{Value:"CA",Name:"CA"},
{Value:"FL",Name:"FL"},
{Value:"NY",Name:"NY"},
{Value:"VG",Name:"VG"},
{Value:"TX",Name:"TX"}
]);
var response = [{ }];
self.CartItems($.map(response, function (item) {
return new AppViewModel(item);
}));
self.TaxedItems = ko.observable();
self.SelectedState = ko.observable(0);
self.SelectedState.subscribe(function (StateValue) {
if(StateValue ==='FL'){
self.TaxedItems('this should appear');
}else{
self.TaxedItems('');
}
})
}
function AppViewModel(data) {
var self = this;
// ...
}
ko.applyBindings(new MainViewModel());
View :看法 :
<select data-bind="foreach: States ,value:SelectedState">
<option data-bind="visible:$index()==0" value="0" disabled>Select Tax State..</option>
<option data-bind="value: Value,text:Name"></option>
</select>
<br>
<span data-bind="text: TaxedItems"></span>
Example : http://jsfiddle.net/GSvnh/5095/示例: http : //jsfiddle.net/GSvnh/5095/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.