mercredi 20 janvier 2016

ASP MVC: Getting checked checkbox value and getting sum of input hidden when checkbox is checked

I am creating a ASP page with table form for a pre-enrollment system where the contents are as follows:

Column 1: Grades

Column 2: Checkbox (value is dynamically generated from SQL Server)together with a Input hidden (value is equal to the subject units)

Column 3: Subject Code

Column 4: Subject Description

Column 5: Subject Units

Column 6: Subject Pre-requisite

The view of the page is here: Page view

What I want to happen is to get the sum of the checked checkbox through the value of input hidden that goes after the checkbox. Since the value of the checkbox (subject code) will be used in the post.

Here is the format of the checkbox and hidden:

<input type="checkbox" name="BSCS" value="@courses.course_code" onchange="checkUnits()" style="height:16px; width:16px;" />
<input type="hidden" name="BSCS_units" value="@courses.subject_unit" />

I tried to use javascript and it display on the text field the value "NaN"

Here is the script:

<script>
function checkUnits() {
    document.AdvisingForm.total.value = '';
    var sum = 0;
    var maxUnits = "@Html.Raw(Json.Encode(ViewBag.maxUnit))";
    for (i = 0; i < document.AdvisingForm.BSCS.length; i++) {
        if (document.AdvisingForm.BSCS[i].checked) {
            sum = sum + parseInt(document.AdvisingForm.BSCS_units[i].value);

            if (sum >= +maxUnits + 2) {
                alert("You have already exceeded the maximum number of units required for this semester. Remove one of the courses selected to proceed on next step.");
                $('#course-BSCS').attr('disabled', 'disabled');
            }
            else
            {
                $('#course-BSCS').removeAttr('disabled');
            }
        }      
    }

    document.AdvisingForm.total.value = sum;
}
</script>

I'm really confused with the script part. How can I accomplish this one. Any help will be appreciated. Thanks a lot.




Aucun commentaire:

Enregistrer un commentaire