Grails, cannot get value from checkbox in controller

I added a checkbox to the template and used AJAX to control the changes. When the checkbox changes, the action in the controller is called where I want different things depending on the checkbox setting. But I don't see if it's checked or not. I print a list of options and there you can see if the checkbox is checked or not, but I cannot use:

If (checkbox.checked) {do something} else {do โ€‹โ€‹something else}

Even if the box is not checked, I always get {do something}.

Heres som code:

<script>
    $(document).ready(function(){
        $( document ).on('change', '.useWeeklyVolumes', function ( event ){ 
            $.ajax({
                url: '${g.createLink( controller:'offerDetail', action:'useWeeklyVolumes' )}',
                data: {ckbWeeklyVolumes:this.checked, id:this.id},
                type: 'get'
            }).success( function ( data ) { $( '#nono' ).html( data );     });
        });
    });
</script>

      

Part of the template:

<label for="ckbWeeklyVolumes">
    <g:message message="Specify volumes weekly"/>
</label>
<g:checkBox id="${offerDetail.id}" class="useWeeklyVolumes" name="ckbWeeklyVolumes" value="${offerDetail?.useWeeklyVolumes}" />

<g:if test="${offerDetail?.useWeeklyVolumes}">
---- Here the part of the template which is controlled by the checkBox
</g:if>

      

Controller action method:

def useWeeklyVolumes() {
println("useWeeklyVolumes - params: "+params)
def od = OfferDetail.get(params.id)
od.useWeeklyVolumes = params.ckbWeeklyVolumes
od.save(flush:true)
if (od.useWeeklyVolumes) {
    println("useWeeklyVolumes - ON: ")
} else {
    println("useWeeklyVolumes - OFF: ")
}
render(template: "offerDData", model: [offerDetail: od])

      

}

Console printout:

useWeeklyVolumes - params: [ckbWeeklyVolumes:true, id:30, controller:offerDetail, format:null, action:useWeeklyVolumes]
useWeeklyVolumes - ON: 
useWeeklyVolumes - params: [ckbWeeklyVolumes:false, id:30, controller:offerDetail, format:null, action:useWeeklyVolumes]
useWeeklyVolumes - ON: 

      

+3


source to share





All Articles