问题描述
目前在下面的代码中,我有4个动态值,最终用户将从组合框中选择度,输入值一个学期,最大分数和获得分数作为数值.输入后,将创建动态行,并且在单击总和"时还将通过公式(om/mm)* 100计算每一行的百分比,并且还会创建新列……当度数仅发生变化时,我需要有代码.计算每个学期的所有最大分数和获得分数的总和,然后按上述公式计算百分比……而且不应创建新列……..请以最大分数排序……在这方面请帮助我……谢谢您的帮助最初是我.
欢呼声
桑索什
Presently in this below code, I have 4 dynamic values, end user will be select degree from combo box, entering the values semester, Max marks and Obtained marks as numeric values. Once entered dynamic rows will be created and it also calculate the percentage by the formula (om/mm)*100 for each row on clicking Sum and it also creates new column… I needed to have code when the degree changes only.. it should calculate the sum of all max.marks and Obtained marks of each semester and then calculate percentage by above formula… and also new column should not be created…..Please sort with max marks… Please help me in this regard… Thank you for helping me initially.
Cheers
Santhosh
function insRows()
{
var x = document.getElementById('finalMarks').insertRow();
var cell = x.insertCell(0)
var a = x.insertCell(1);
var b = x.insertCell(2);
var c = x.insertCell(3);
var d = x.insertCell(4);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell.appendChild(element1);
a.innerHTML = document.getElementById('txtqualify').options[document.getElementById('txtqualify').selectedIndex].text
b.innerHTML = document.getElementById('txtsem').value
c.innerHTML = document.getElementById('txtmaxmrk').value
d.innerHTML = document.getElementById('txtobtmrk').value
}
function function1()
{
var myRow = document.all.finalMarks.rows.length;
var ed='';
var mm=0,om=0, x,z;
for(i=0;i<myRow;i++)
{
r = document.all.finalMarks.rows[i];
pr = r.cells[1].innerHTML
mm+= parseInt(r.cells[3].innerHTML)
om+= parseInt(r.cells[4].innerHTML)
if (pr!=ed && ed!='')//(i==myRow-1)
{
prevr = document.all.finalMarks.rows[i-1];
mycurrent_cell = document.createElement("td");
mm-= parseInt(r.cells[3].innerHTML)
om-= parseInt(r.cells[4].innerHTML)
z = (om/mm)*100
mycurrent_cell.innerHTML = z
mm= parseInt(r.cells[3].innerHTML)
om= parseInt(r.cells[4].innerHTML)
alert(mycurrent_cell.innerHTML + '[' + pr + ']' + ed)
//alert(!isNaN(mycurrent_cell.innerHTML))
if(!isNaN(mycurrent_cell.innerHTML))
{
qualary[qualary.length] = prevr.cells[1].innerHTML
pctary[pctary.length] = mycurrent_cell.innerHTML
prevr.appendChild(mycurrent_cell);
mycurrent_cell.innerHTML = z;
document.getElementById('txtpert').value = z
}
}
ed = r.cells[1].innerHTML
alert(ed)
if (i==myRow-1)
{
prevr = document.all.finalMarks.rows[i];
//mycurrent_cell = document.createElement("td");
x = (om/mm)*100
mycurrent_cell.innerHTML = x
qualary[qualary.length] = prevr.cells[1].innerHTML
pctary[pctary.length] = mycurrent_cell.innerHTML
prevr.appendChild(mycurrent_cell);
if(!isNaN(x))
document.getElementById('txtperct').value = x
}
}
//document.frmMarks.txtper.value = Math.max( x, z)
var str = sortAndReturn(pctary,qualary)
//alert(pctary + '$' + qualary) //alert(sortAndReturn(pctary,qualary))
//alert(str) //alert(str[0]+','+str[1])
document.getElementById('degr').value = str.split('$')[0]
document.getElementById('txtmarks').value = str.split('$')[1]
}
function sortAndReturn(pctary,qualary)
{
var myArray3 = new Array();
for(i=0;i<pctary.length;i++)
myArray3[i]=pctary[i];
sort(pctary)
//alert(sort(pctary)) //alert(myArray3)
return qualary[indexSearch(myArray3,pctary[0])] + '$' + pctary[0]
}
function sort(a)
{
var max;
for(i=0;i<a.length;i++)
{
max = i;
for(j=i+1;j<a.length;j++)
{
if (a[j]>a[max])
{
max=j;
}
}
//alert(a[max])
if (i != max)
{
var swap = a[i];
a[i] = a[max];
a[max] = swap;
}
}
}
function indexSearch(a,b)
{
for(i=0;i<a.length;i++)
if(a[i]==b)
{return i ;}
}
HTML代码:
HTML CODE:
<table BORDER ="6" width="85%"align="center" bgcolor="#00FFFF" style="font-family: Verdana;color: black;font-size: 12px">
<tr>
<td align="center" width="25%" height="40"> Qualification:</td>
<td align="left" style="WIDTH:25%" >
<select id ="txtqualify" style ="width:150px;height:25px" name ="txtqualify" > </select> </td>
<td align="center" width="25%" height="40">Semester:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtsem" name = "txtsem"> </td>
</tr>
<tr>
<td align= "center" width ="25%" height="40">Max.Marks:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtmaxmrk" name = "txtmaxmrk"> </td>
<td align= "center" width ="25%" height="40">Obtained.Marks:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtobtmrk" name = "txtobtmrk"> </td>
</tr>
<tr>
<td align= "center" width ="25%" height="40">Percentage:</td>
<td> <input type = "text" SIZE="8" maxlength = "5" id = "txtper" readonly name = "txtper">
<td align="center" colspan="4" valign="middle">
<input type="RESET" align="middle" style="width:20%" value="ADD" onclick="insRows()" >
<input type="button" align="middle" style="width:20%" value="REMOVE" onclick="delRow()" >
<input type="button" align="middle" style="width:20%" value="CLOSE" onclick="" >
<input type="button" align="middle" style="width:20%" value="SUM" onclick="function1()" >
</td>
</tr>
<tr>
<td align="center" height="40"> <input type = "text" SIZE="8" maxlength = "5" id = "txtpert" name = "txtpert"> </td>
<td align="center"> <input type = "text" SIZE="8" maxlength = "5" id = "txtperct" name = "txtperct"> </td>
<td align="center"> <input type = "text" SIZE="10" maxlength = "5" readonly id = "degr" name = "degr"> </td>
</tr>
</table>
<table id ="finalMarks" BORDER ="6" width="85%"align="center" bgcolor="#00FFFF" style="font-family: Verdana;color: black;font-size: 12px">
<thead>
<th height="40">Remove</th>
<th>Qualification</th>
<th>Sem/Year</th>
<th>Max.Marks</th>
<th>Obt.Marks</th>
<th>Percentage of Marks:</th>
</thead>
</table>
推荐答案
这篇关于计算动态行的总和和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!