找到学生在JQUERY中有最高分?

问题描述 投票:1回答:1

我用了

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>获取值(我的语法不正确)。请修改它来帮助我。我想强调这个位置的学生有最高分。

jquery html
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.