检查多个复选框Ajax& jquery&春天的mvc

时间:2015-03-04 02:45:53

标签: jquery ajax spring

我尝试检查多个复选框,而不是全部使用Ajax调用。它工作正常但是当我取消选中它们时,只有一个未经检查,其他人再去检查。

Ajax要求检查并取消选中:

   $(function(){
$('input[type=checkbox]').bind("change",function(){ 

if($(":checkbox[path='description']:checked").length > 0 ){
    $.ajax({
        type: 'POST',
        url: 'check.html',
        data: { "id": $(this).attr('cid')},
        success: function(data){
        }

    });
}

else{

    $.ajax({
        type: 'POST',
        url: 'uncheck.html',
        data: { "id": $(this).attr('cid')},
        success: function(data){
        }
    });


}

});

 });

html part,for for循环检查点:

        <c:forEach var = "c" items = "${planid.checkpoints}" varStatus="status">

                         <c:if  test = "${c.st eq s.name and c.rw eq r.name}">

                         <p><input  cid="${c.id}" class= "case" path="description" type="checkbox" value="${c.description}"   />
                         <c:out  value = "${c.description}"></c:out>

                        <security:authorize access="hasRole('admin')">                              
                        <a href="/gefp/jsp/Edit.html?id=${c.id}">[Edit]</a>
                        </security:authorize>

                        </p>

                    </c:if>

                 </c:forEach>

在控制器中:

    @RequestMapping("/jsp/check.html") 
      @ResponseBody
      public  ResponseEntity<String> check(@RequestParam Long id,     HttpSession session){

        String u=SecurityUtils.getUsername();
         User user=userDao.getUser(u).get(0);        
         user.getId();

         Checkpoint cp = userDao.getCheckpoint(id);         

         user.getCheckpoints().add(cp);
         userDao.saveUser(user);

        return new ResponseEntity<String>( HttpStatus.OK );

    }



    @RequestMapping("/jsp/uncheck.html") 
    @ResponseStatus(HttpStatus.OK)
    public void uncheck(@RequestParam Long id){   

         String u=SecurityUtils.getUsername();
         User user=userDao.getUser(u).get(0);        
         user.getId();

        Checkpoint cp = userDao.getCheckpoint(id);
        user.getCheckpoints().remove(cp);
        userDao.saveUser(user);
    }

1 个答案:

答案 0 :(得分:0)

我通过

解决了这个问题
$(function(){
$('input[type=checkbox]').change(function(){ 
    var checked = $(this).is(':checked');

if(checked){
    $.ajax({
        type: 'POST',
        url: 'check.html',
        data: { "id": $(this).attr('cid')},
        success: function(data){
        }

    });
}

else{

    $.ajax({
        type: 'POST',
        url: 'uncheck.html',
        data: { "id": $(this).attr('cid')},
        success: function(data){
        }
    });


}

});

 });