简体   繁体   中英

Disable a checkbox in spring and pass the checked value to controller

Hello I have to disable checkbox in spring html form and pass the checked value to controller. I use the below code, but the problem is that I am not able to pass the checked value to controller when the disabled attribute is set to true

<form:checkbox path="agreementCats" disabled="true" value="${x.value}" class="category-checkboxes" id="agreementCat-cb-${i.index}" /><label class="control-label" for="agreementCat-cb-${i.index}">&nbsp;${x.label}</label>
<form:hidden path="agreementCats" value="${x.value}" />

The above code sends all the values to controller. Please help!!

代替disabled="true"将其更改为readonly= "true"如果您插入,Disabled不会发送值,但是readonly会发送值。

in HTML have two tag with similar meaning but different context is Disabled and ReadOnly.

  • disabled : Disabled the input in the form and the data isn't in form query.
  • readonly : Omit the user UI interaction but the input is in form query.

Try use readonly tag instead disabled, I hope this help you.

That solution works for all inputs except checkbox ... Checkbox can't use readonly, but you can do a little trick, is a dirt trick, but it can you help to not permit user touch check box.

If you know in server side what checkbox can be edited, try this code:

<input type="checkbox" <c:if test="condition">onclick="return false;"</c:if> ></input>

That code not permit user click the checkbox if the condition is true. Maybe with this can solved your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM