javascript - How to calculate html input box values in another input box
问题描述
Following table is accessing values from mysql table in first two columns (exclude sr.no.) and offering to enter 'obtained marks' in 4th column. I have also made three columns after obtained marks which are 'percentage', 'grade' and remarks. I want to calculate percentage automatically when user enters marks in obtained marks column and navigates to next input box which is in second row:
<table id = "result" class="data-table">
<caption class="title"></caption>
<thead>
<tr>
<th><strong>Sr.No.</strong></th>
<th><strong>Student ID</strong></th>
<th align="center"><strong>Student Name</strong></th>
<th style="text-align: center;"><strong>Obtained Marks</strong></th>
<th style="text-align: center;"><strong>Percentage</th>
<th style="text-align: center;"><strong>Grade</strong></th>
<th style="text-align: center;"><strong>Remarks</strong></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query)) {
$stu = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
echo '<tr>
<td>'.$no.'</td>
<td>'.$row['student_id'].'</td>
<input type="hidden" name="student_id[]" value='.$row['student_id'].'>
<td style="text-align: left;">'.$row['student_name'].'</td>
<input type="hidden" name="student_name[]" value='.$row['student_name'].'>
<td>'."<input name='obtmarks[]' placeholder='' class='form-control' type='number' required='required' style='width: 120px;'>".'</td>
<td>'."<input name='percentage[]' placeholder='' class='form-control' type='number' required='required' style='width: 120px;'>".'</td>
<td>'."<input name='grade[]' placeholder='' class='form-control' type='number' required='required' style='width: 120px;'>".'</td>
<td>'."<input name='remarks[]' placeholder='' class='form-control' type='number' required='required' style='width: 120px;'>".'</td>
<input type="hidden" name="class[]" value='.$row['class'].'>
<input type="hidden" name="test_date[]" value='.$test_Date.'>
<input type="hidden" name="test_subject[]" value='.$Subject_type.'>
<input type="hidden" name="test_type[]" value='.$TestType.'>
</tr>';
$total += $row['stu_id'];
$no++;
}
?>
</tbody>
</table>
解决方案
you can simply achieve this by using <input type="text"/>
event of onkeyup('calculateFn()')
. For this, you have Total Marks too to get that percentage out for your obtained marks.
using this script
<script>
var obmarks=document.getElementById("obmarks");
var totalmarks=document.getElementById("totalmarks");
var percent=(obmarks/totalmarks)*100;
document.getElementById("percentage").value=percent;
</script>
here ids are those corresponding ids of required text fields. Hope this help you out.
推荐阅读
- python - 按第二个值对字典列表进行排序
- c# - 如何以小时、分钟和秒为单位找到时差 C# windows form app
- c# - 如何在 C# 中合并两个自定义类型列表
- php - 没有显示它需要显示的内容
- spring-boot - 无法访问 Openshift 上已部署的 Spring Boot 应用程序 (war) 的端点
- python - BS4 在 Python 中没有定位元素
- python - 如何从数据帧生成频率表?
- bash - 如何使用 bash 在 Windows 上的 VSC 中自动激活 conda env?
- html - 不同表格单元格中的两个选择选项不起作用
- cmake - CMake 设置变量