Reputation: 91
Through following php code I am getting some values from php and displaying in table.
<?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'].'>
<input type="hidden" name="Maxmarks[]" value='.$MaxMarks.'>
<td>'."<input name='obtmarks[]' placeholder='' class='form-control obtmark' type='number' required='required' maxlength='2' style='width: 120px;'>".'</td>
<td>'."<input name='percentage[]' placeholder='' class='form-control percentage' type='text' required='required' style='width: 120px;'>".'</td>
<td>'."<input name='grade[]' placeholder='' class='form-control grades' type='text' required='required' style='width: 120px;'>".'</td>
<td>'."<input name='remarks[]' placeholder='' class='form-control remark' type='text' required='required' style='width: 120px;'>".'</td>
<input type="hidden" name="class[]" style="text-align: center;" value='.$row['class'].'>
<input type="hidden" name="test_date[]" value='.$TestDate.'>
<input type="hidden" name="test_subject[]" align="center" value='.$SelectSubject.'>
<input type="hidden" name="test_type[]" align="center" value='.$TestType.'>
</tr>';
$total += $row['stu_id'];
$no++;
}
?>
Following jquery code is to restrict the user to enter obtained marks less than or equal to max marks.
<script>
$('[name="obtmarks[]"]').keyup(function(){
if(parseInt($(this).val()) > '[name="Maxmarks[]"]'){
$('#div1').html('value cannot be greater then 25');
$(this).val('');
}
else if(parseInt($(this).val()) < 0)
{
$('#div1').html('value cannot be lower then 0');
$(this).val('');
}
else
{$('#div1').html('');}
});
</script>
The code is working if i am giving 25 instead of '[name="Maxmarks[]"]' , i am getting Maxmarks from html post method against a variable $MaxMarks. How to replace maximum marks value by getting it from '[name="Maxmarks[]"]'.
Upvotes: 1
Views: 29
Reputation: 337560
You have two issues here. Firstly you're comparing an integer against a selector string. You need to actually create a jQuery object from that string to retrieve the element, then get its value before making the comparison.
Secondly, you will have multiple name="Maxmarks[]"
inputs, so your current selector would always compare against the first one. From the structure of your table it would seem that you instead want to compare the marks on the current row instead. As such you need to use DOM traversal to find the related Maxmarks
element. You can do that by using closest()
to get the nearest common tr
element, then find()
. Try this:
$('[name="obtmarks[]"]').keyup(function() {
var $div1 = $('#div1');
var $obtMarks = $(this);
var marks = parseInt($obtMarks.val(), 10);
var maxMarks = parseInt($obtMarks.closest('tr').find('[name="Maxmarks[]"]').val(), 10);
if (marks > maxMarks) {
$div1.html('value cannot be greater than 25');
$obtMarks.val('');
} else if (marks < 0) {
$div1.html('value cannot be lower than 0');
$obtMarks.val('');
} else {
$div1.html('');
}
});
Finally, note that the HTML you're generating is invalid; the hidden
input fields need to be contained within a td
in the table
.
Upvotes: 1