dimanche 16 juillet 2017

I need value of checkbox on checked as well as on unchecked

I need to get value of check box on checked as well as on unchecked. But in my controller i only got the value of checked.

attendance.jsp

        </tr>
              <c:forEach items="${stlist}" var="x">
                         <tr>
                             <td>${x.roll}<input type="text" value="${x.roll}" name="roll" hidden/> </td>
                             <td>${x.sfirstname} ${x.slastname}</td>
                                                                <td><center><input type="checkbox" name="attendance" onchange="checkAttendance(this);"/></center></td>
                              <td><input type="text" name="remarks" /></td>
                          </tr>
        </c:forEach>
<script language="JavaScript">
 $(function () {
                                function toggle(source) {
                                    checkboxes = document.getElementsByName('attendance');
                                    for (var i = 0, n = checkboxes.length; i < n; i++) {
                                        checkboxes[i].checked = source.checked;
                                    }
                                }
                                ;

                                function checkAttendance(event) {
                                    console.log($(event));
                                    if($(event).is(':checked')){
                                        console.log("checked true");
                                        $(event).attr('value', 'true');
                                    }else{
                                        $(event).attr('value', 'false');
                                    };
                                }


                            </script>

TeacherController.java

        @Controller
        public TeacherController {
        @RequestMapping(value = "/insertattendance", method = RequestMethod.POST)
            public String insertattendance(@RequestParam("roll") String roll[], @RequestParam("subjectcode") String subjectcode, @RequestParam("semesterid") String semesterid, @RequestParam(value = "attendance", required = false) String attendance[], @RequestParam("remarks") String remarks[], @RequestParam("date") String date, Model model) {

                int semid = Integer.parseInt(semesterid);
                System.out.println("===============" + roll.length);
                model.addAttribute("msg", "Data saved");


                for (int i = 0; i < roll.length; i++) {
                    boolean check = false;
                    try {
                        if ("true".equalsIgnoreCase(attendance[i])) {
                            check = true;
                        }else{
                            check = false;
                        }
                        System.out.println("check===" + check);

                    } catch (Exception e) {
        //                System.out.println("===========" + e);
                        check = false;
                    }
                    Attendance a = new Attendance(0, roll[i], semid, date, subjectcode, check, remarks[i]);
                    teacherDao.insertattendance(a);
                }

                return "redirect:/attendance";
            }
        }

Attendance.java

    public class Attendance{
       // attributes as per in constructor and id is autoincrement + primary key
    public Attendance() {
        }

     public Attendance(Integer id,String roll, Integer semesterid, String date,  String subjectcode, Boolean attendance, String remarks) {
            this.id = id;
            this.date = date;
            this.attendance = attendance;
            this.remarks = remarks;
            this.roll = roll;
            this.subjectcode = subjectcode;
            this.semesterid = semesterid;
        }
    }

When i try to insert the attendance and in attendance.jsp if i unchecked any one checkbox i got error ArrayIndexOutOfBound cause in controller i didnt get the value of unchecked checkbox.




Aucun commentaire:

Enregistrer un commentaire