找出学生在JQUERY中的最高分?

时间:2019-04-13 14:15:25

标签: jquery html

我用过

var average_point = "<tr><td>" + sv[2] + "</td>" + "<td>" + sv[3] + "</td>" +"<td>" + sv[4] + "</td></tr>";

我想从HTML中的单个元素获取价值。 但是此代码不正确。 如何从HTML中获取价值?

HTML

<tbody id="list-student">
</tbody>

jQuery

这些代码将元素添加到数组中,并通过HTML上的<tbody></tbody>显示。 我使用HTML来做到这一点。 (我是新手,所以我会尽力而为。)

                          // math // english // literature
var student = [mssv, ten, d_toan,   d_anh,     d_van,      khoi]; // student array
dssv.push(student); // list stduent
var html = "";
        for (var i = 0; i < dssv.length; i++) {
            var sv = dssv[i];
            var stt = i + 1;
            var sv_html = "<tr><td>" + stt + "</td><td>" + sv[0] + "</td><td>" + sv[1] +
                "</td><td>" + sv[2] + "</td><td>" + sv[3] + "</td><td>" +
                sv[4] + "</td><td>" + sv[5] + "</td><td>" + "<button>delete</button>" + "</td></tr>";
            html = html + sv_html;
        }
        $('list-student').html(html);
// Have button with id="findStudentBtn" in HTML
$("#findStudentBtn").click(function() {
        var dssv = []; // list student

//var mssv = $("#mssv").val(); // do not care about this line :))
//var ten = $("#ten").val(); // do not care about this line

        var d_toan = $("#d-toan").val(); // Math point
        var d_anh = $("#d-anh").val(); // English point
        var d_van = $("#d-van").val(); // Literature point
//var khoi = $("#khoi").val(); // do not care about this line

        var student = [mssv, ten, d_toan, d_anh, d_van, khoi]; // student array
        dssv.push(student);  // push student array in another array
        var max_point = 1;
        var average_point = 0;
        var j = 0;

        for (var i = 0; i < dssv.length - 1; i++) {
            j = i + 1;
            var student = dssv[i];

// this line is an issue.
            var average = "<tr><td>" + sv[2] + "</td>" + "<td>" + sv[3] + "</td>" +"<td>" + sv[4] + "</td></tr>";

                average_point = average / 3;
            while (i < j && j < dssv.length - 1) {
                while (sv[i].average_point < sv[j]..average_point && j < dssv.length - 1) {
                    max_point = sv[j].average_point;
                    j++;
                }
                i++;
                j = i + 1;
            }

        }
    alert('The student has highest point: ' + max + ' point');

如何为职位学生的CSS设置最高点。 该代码无效。

     // $('#ds-sv').position().css({"background-color": "red"});
    });

我想要的最终结果是: 1.从单个<tr></tr>中获取值(我对语法有误)。请修正它以帮助我。 2.我要突出显示该职位的学生得分最高。

1 个答案:

答案 0 :(得分:0)

检查我的代码。

function init(){
	var dssv = [];
	var points = [];

	var student = ['mssv', 'ten', 'd_toan','d_anh','d_van','khoi'];

	var html = "";
	for (var i = 0; i < student.length; i++) {
			var counter = i+1;
			html +="<tr id='"+student[i]+"'><td>" + student[i] + "</td><td>" + counter + "</td><td><button id='dltStud'>Delete</button></td></tr>";
		    points.push(counter);
	}
		    
	var iterator = points.values();
				
    let highest = points[0];
	for (let elements of iterator) { 
        if (highest < elements){
            highest = elements;
        }
    } 
			
	document.querySelector("#list-student > tbody").innerHTML = html;
        
    $("#list-student > tbody > tr > td").each(function(){
         let el = $(this).html();
		 if(el == highest){
             $(this).parent().css({'font-style':'italic','color':'red'});
         }
    });
  
}
			
addEventListener('load',init);
#list-student{
			border-spacing: 15px;
      border-collapse: separate;
		}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="list-student" cellspacing="10">
		<thead>
			<th>Student</th>
			<th>Score</th>
		</thead>
    <tbody>
		</tbody>
	</table>