gpt4 book ai didi

javascript - Knockout.js 正在将数字转换为字符串

转载 作者:行者123 更新时间:2023-11-28 13:28:46 25 4
gpt4 key购买 nike

我正在尝试做一个简单的时间表演示,但我的计算可观察量遇到了一些问题,这些问题坚持将我的数字转换为字符串并将它们连接起来而不是相加。 JavaScript 不是我的专长。

代码是:

var ViewModel = function() {
self = this;
this.RegSun = ko.observable(0);
this.RegMon = ko.observable(0);
this.RegTue = ko.observable(0);
this.RegWed = ko.observable(0);
this.RegThu = ko.observable(0);
this.RegFri = ko.observable(0);
this.RegSat = ko.observable(0);
this.HolSun = ko.observable(0);
this.HolMon = ko.observable(0);
this.HolTue = ko.observable(0);
this.HolWed = ko.observable(0);
this.HolThu = ko.observable(0);
this.HolFri = ko.observable(0);
this.HolSat = ko.observable(0);

this.RegSun.extend({ required: true, min: 0, max: 24 });
this.RegMon.extend({ required: true, min: 0, max: 24 });
this.RegTue.extend({ required: true, min: 0, max: 24 });
this.RegWed.extend({ required: true, min: 0, max: 24 });
this.RegThu.extend({ required: true, min: 0, max: 24 });
this.RegFri.extend({ required: true, min: 0, max: 24 });
this.RegSat.extend({ required: true, min: 0, max: 24 });
this.HolSun.extend({ required: true, min: 0, max: 24 });
this.HolMon.extend({ required: true, min: 0, max: 24 });
this.HolTue.extend({ required: true, min: 0, max: 24 });
this.HolWed.extend({ required: true, min: 0, max: 24 });
this.HolThu.extend({ required: true, min: 0, max: 24 });
this.HolFri.extend({ required: true, min: 0, max: 24 });
this.HolSat.extend({ required: true, min: 0, max: 24 });

this.RegTot = ko.computed(function() {
return self.RegSun() + self.RegMon() + self.RegTue() + self.RegWed() + self.RegThu() + self.RegFri() + self.RegSat();
});
this.HolTot = ko.computed(function() {
return self.HolSun() + self.HolMon() + self.HolTue() + self.HolWed() + self.HolThu() + self.HolFri() + self.HolSat();
});
this.TotSun = ko.computed(function() {
return self.RegSun() + self.HolSun();
});
this.TotMon = ko.computed(function() {
return self.RegMon() + self.HolMon();
});
this.TotTue = ko.computed(function() {
return self.RegTue() + self.HolTue();
});
this.TotWed = ko.computed(function() {
return self.RegWed() + self.HolWed();
});
this.TotThu = ko.computed(function() {
return self.RegThu() + self.HolThu();
});
this.TotFri = ko.computed(function() {
return self.RegFri() + self.HolFri();
});
this.TotSat = ko.computed(function() {
return self.RegSat() + self.HolSat();
});
this.TotTot = ko.computed(function() {
return self.RegTot() + self.HolTot();
});
};

HTML 是:

<table class="workspacetable">
<thead><tr><th>&nbsp;</th><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th><th>Total</th></tr></thead>
<tr><td>Regular</td><td><input type="text" data-bind="value: RegSun" class="timesheethours" /></td><td><input type="text" data-bind="value: RegMon" class="timesheethours" /></td><td><input type="text" data-bind="value: RegTue" class="timesheethours" /></td><td><input type="text" data-bind="value: RegWed" class="timesheethours" /></td><td><input type="text" data-bind="value: RegThu" class="timesheethours" /></td><td><input type="text" data-bind="value: RegFri" class="timesheethours" /></td><td><input type="text" data-bind="value: RegSat" class="timesheethours" /></td><td><span data-bind="text: Number(RegTot())">&nbsp;</span></td></tr>
<tr><td>Holiday</td><td><input type="text" data-bind="value: HolSun" class="timesheethours" /></td><td><input type="text" data-bind="value: HolMon" class="timesheethours" /></td><td><input type="text" data-bind="value: HolTue" class="timesheethours" /></td><td><input type="text" data-bind="value: HolWed" class="timesheethours" /></td><td><input type="text" data-bind="value: HolThu" class="timesheethours" /></td><td><input type="text" data-bind="value: HolFri" class="timesheethours" /></td><td><input type="text" data-bind="value: HolSat" class="timesheethours" /></td><td><span data-bind="text: HolTot">&nbsp;</span></td></tr>
<tr><td><b>Total</b></td><td><span data-bind="text: TotSun">&nbsp;</span></td><td><span data-bind="text: TotMon">&nbsp;</span></td><td><span data-bind="text: TotTue">&nbsp;</span></td><td><span data-bind="text: TotWed">&nbsp;</span></td><td><span data-bind="text: TotThu">&nbsp;</span></td><td><span data-bind="text: TotFri">&nbsp;</span></td><td><span data-bind="text: TotSat">&nbsp;</span></td><td><span data-bind="text: TotTot">&nbsp;</span></td></tr>
</table>

我有一把 fiddle here

如果您在 fiddle 中输入某个日期的值,问题应该会立即显现出来。我肯定错过了一些简单的东西。

最佳答案

它们是字符串,因为它们绑定(bind)到接受字符串的文本框。您需要将它们转换回数字。像这样的东西:

this.TotSun = ko.computed(function() {
return parseInt(self.RegSun(),10) + parseInt(self.HolSun(),10);
});

参见parseInt 。注意:如果您需要支持浮点值,请查看 parseFloat

但正如 @BenjaminGruenbaum 在评论中所说,你确实需要学习使用数组。

关于javascript - Knockout.js 正在将数字转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26788673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com