简体   繁体   English

在重新加载JSP时保持复选框状态

[英]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. 我有一些代码,当我从控制器回来后重新加载jsp页面时,我想保留多个复选框状态(已选中)。我可以在会话中成功检索选定的值,但无法选中复选框。 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. 首先使用document.ready仅在页面完全加载时执行此操作。 also, use .prop() for setting the check state 另外,使用.prop()设置检查状态

// 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 您还应该确保$("#\\"removebasket"+<%=portarray[i]%>+"\\"")正确无误,方法是在重新加载页面时查看其源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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