I added a checkbox in a template and used AJAX to supervice changes. When the checkbox cahnges an action in the controller is called where I want to different things depending on the checkbox setting. But I can't see if it is checked or not. I prints the params list and there you can see if the checkbox is checked or not but I can't use:
If (checkbox.checked) { do something} else {do something else}
Even if checkbox is unchecked I always get to {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>
The 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:
Aucun commentaire:
Enregistrer un commentaire