【发布时间】:2020-01-17 15:56:57
【问题描述】:
在计算用户输入的数字的总和并自动显示时,我需要一些帮助。当用户删除一个数字时,总和也应该自动计算。
$('.col-lg-1').change(function() {
var total = 0;
$('.col-lg-1').each(function() {
if ($(this).val() != '') {
total += parseInt($(this).val());
}
});
$('#result').html(total);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-lg-1">
<label>TEST 1 </label>
<input type="hidden" name="section_id" value="<?php echo $section_id; ?>">
<input type="hidden" name="session_id" value="<?php echo $session_id; ?>">
<input type="hidden" name="student_id" value="<?php echo $student_id; ?>">
<input type="hidden" name="class_id" value="<?php echo $class_id; ?>">
<input type="number" name="mt_ca1" class="form-control input-sm rounded-0" required>
</div>
<div class="col-lg-1" id="t2">
<label>TEST 2</label>
<input type="number" name="mt_ca2" class="form-control input-sm rounded-0" required>
</div>
<div class="col-lg-1" id="assg">
<label>TEST 3</label>
<input type="number" name="mt_ca3" class="form-control input-sm rounded-0" required>
</div>
<div class="col-lg-1">
<label>TOTAL</label>
<output id="result"></output>
</div>
【问题讨论】:
-
.col-lg-1是一个 DIV - 您正在附加一个更改事件并尝试从 DIV 中获取this.value... -
更改输入中的某些内容实际上会触发父 div 中的更改事件,因为它会冒泡。不起作用的是
$(this).val(),因为 div 没有价值。
标签: javascript function codeigniter input