user3591738
user3591738

Reputation: 55

JSTL checkbox not working

Situation: When we tick the checkbox, a post request is send with checkbox value as false And we set the Model as true in the validation.

Issue
But the issue is this Model object which is also a session attribute do not reflect this change on the jsp. so the issue is obj.value is always false in both situations ?

<div class="block">
<label class="medium">Attach authority:</label>
<input type="checkbox"  value="${Model.noticeDetailsModel.fullAuthority}" 
     <c:if test="${Model.noticeDetailsModel.fullAuthority}">checked="checked"</c:if> 
    id="fullAuthority" onchange="javascript: setNoticeDetails(this);"/>
</div>

//set up details
function setNoticeDetails(obj) {
    $.post("generateNotice.do", {
        value : obj.value,
        name : obj.id,
        stage : "setNoticeDetails"
    });
}

Situation 2: When we tick off the checkbox, a post request is again send with checkbox value as false rather than true ( as we set the value as true in the model object namely "Model") after the first request was send

Upvotes: 0

Views: 574

Answers (1)

Louise Miller
Louise Miller

Reputation: 3199

As I cant see the Controller code, I assume that the java side, i.e receiving of post request works ok

However :

Your function setNoticeDetails sends an ajax request, the response of which is ignored by your code because you have not implemented a success handler. See http://api.jquery.com/jquery.post/ for more details.

So because its an ajax request, the html/page is not reloaded - instead you should manipulate the html through javascript - e.g if you just want to change the value of the checkbox, do this :

function setNoticeDetails(obj) {
    $.post("generateNotice.do", {
        value : obj.value,
        name : obj.id,
        stage : "setNoticeDetails",
        success:  function(data) { 
             if ( $("#fullAuthority").val() === "true" ){
                $("#fullAuthority").val("false"); 
             } else {
                $("#fullAuthority").val("true");
             }
        } 
        }); 
    }

Upvotes: 1

Related Questions