简体   繁体   中英

Persist checkbox state while jsp reload

I have code where I want to keep more than one checkboxes state(checked) when the jsp page reloads after coming back from controller.I can successfuly retrieve the selected values in sesssion but am unable to get the check boxes checked. My code snippet is as follows:

<%!public String portarray[] = null;%>  
<%if (request.getSession().getAttribute("portselected") != null) {
            portarray = (String[]) request.getSession().getAttribute("portselected");
for (int i = 0; i < portarray.length; i++) {%>
  alert(
    <%=portarray[i]%>
  );
$("#\"removebasket"+<%=portarray[i]%>+"\"").attr("checked", true);
$('#removebasket13989')[0].checked = true;

firstly use document.ready to only do this when the page is fully loaded. also, use .prop() for setting the check state

// only when page fully loads
$(document).ready(function() {
   // use prop
   $('.myCheckbox').prop('checked', true);
});

you should also make sure that $("#\\"removebasket"+<%=portarray[i]%>+"\\"") is correct byt viewing the source of your page when it reloads

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