我有四个用户插入值的文本字段。对于前两个输入字段,它会计算将两个字段一起添加。我们的想法是获得每个字段的百分比值。因此,一旦有值,我们除以总数,再乘以100得到每个文本字段的百分比值。
一切正常!
但是,如果用户手动进入并添加了一天百分比,则应该通过获取该值并减去100来自动计算夜间百分比(或相反)。
每次更改值时,它都会恢复为旧版本。
var day_value, night_value, day_percentage, night_percentage;
$('#day_kwh').on('input', function() {
day_value = this.value;
});
$('#night_kwh').on('input', function() {
night_value = this.value;
});
$('.storage').on('change', function() {
//console.log(day_value + night_value);
if (typeof day_value != 'undefined' && typeof night_value != 'undefined') {
total = parseInt(day_value) + parseInt(night_value);
console.log('total value: ' + total);
day_value = (day_value / total) * 100;
console.log('day value: ' + day_value);
night_value = (day_value / total) * 100;
console.log('night value: ' + night_value);
$('#day_percent').val(day_value);
$('#night_percent').val(night_value);
}
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="storage">
Day kWH:
<input type="text" name="day_kwh" id="day_kwh">
<br>Night kWH:
<input type="text" name="night_kwh" id="night_kwh">
<br>Day %:
<input type="text" name="day_percent" id="day_percent">
<br>Night %:
<input type="text" name="night_percent" id="night_percent">
<br>
<input type="submit" value="Submit">
</div>
&#13;
答案 0 :(得分:1)
您需要添加额外的代码来检查更改的控件是否为day_percent
before <- subset(df1, NUM==records$ID & Date==records$Date & Time<records$Start)
Warning messages:
1: In NUM == records$ID :
longer object length is not a multiple of shorter object length
2: In Date == records$Date :
longer object length is not a multiple of shorter object length
3: In Time < records$Start :
longer object length is not a multiple of shorter object length