I have a table in asp.net mvc . in this table I have a checkbox for every row . I want when I check a checkbox find wage value in that row and sum with other rows wages that checked .
I want to do this sum via Jquery or java script .
@foreach (var item in Model)
{
<tr id="rowid">
<td>
<input type="checkbox" name="chk-@intCount" id="chk-@intCount" class="chkclass" />
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.TechnicalNo)
</td>
<td class="sum" id="wagein">
@Html.DisplayFor(modelItem => item.Wage)
</td>
<td class="sum" id="time">
@Html.DisplayFor(modelItem => item.Time)
</td>
</tr>
}
this is my code in asp.net mvc . how can I sum checked values in wage and time now ?
EDIT :
My jQuery code :
<script src="/Scripts/jquery-1.4.4.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.chkclass').click(function() {
var sum = 0;
$('.chkclass:checked').each(function() {
sum += parseFloat($(this).closest('tr').find('.wagein').text());
});
$('#sum').html(sum);
});
});
</script>
my html code :
<tr>
<td>
<input type="checkbox" name="chk-1" id="chk-1" class="chkclass" />
</td>
<td class="sum wagein">
10
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="chk-2" id="chk-2" class="chkclass" />
</td>
<td class="sum wagein">
10
</td>
</tr>
any idea ?! what is the problem ?!
Start by fixing your markup as it is highly broken. Remember that ids must be unique throughout your entire HTML document:
then you could subscribe to the
.click()event of the checkboxes and accumulate a sum for all wages:And here’s a live demo to see it in action: http://jsfiddle.net/pbkjr/