简体   繁体   English

将setAttribute设置为带有servlet的复选框

[英]setAttribute to checkbox with servlet

here is what I have: 这是我所拥有的:

JSP: JSP:

 <input type="checkbox" name="no_del_file" <% 
if ("True".equals(request.getParameter("no_del_file"))) {
    out.print("checked=\"checked\"");

} %>/>

<%String test = request.getParameter("no_del_file"); %>


 <p><%=test%></p>

Java: Java:

 boolean cbState = request.getParameter( "no_del_file" ) != null;
            System.out.println("cbstate: "+cbState);
            if (cbState == true) {
                request.setAttribute("no_del_file", "checked");
                String checker=(String) request.getAttribute("no_del_file");
                System.out.println(checker);
            }

 RequestDispatcher dispatcher = request.getRequestDispatcher("/runButtonCommand.jsp");
            dispatcher.forward(request, response);

The problem is that the output is: 问题是输出是:

 cbstate: true
 checked

The test gives "on" as print after submitting the form 提交表格后,测试将“ on”打印出来

but the checkbox is not checked itself after the servlet returns the responds. 但是在servlet返回响应之后,复选框本身不会被选中。 The tick is removed for some reason. 剔号由于某种原因被删除。

Any ideas? 有任何想法吗?

Try this 尝试这个

<% if(request.getParameter( "no_del_file" )=="checked"){%>
<input type="checkbox" id="no_del_file" name="no_del_file" value="no_del_file" checked>
<%}%>

I finally got it figured out... If someone is interested here it is 我终于弄明白了...如果有人对这里感兴趣

 <%String test = (String)request.getParameter("no_del_file"); %>
<%String checked = "";%>

<% 
if ("on".equals(test)) {
    checked="checked=\"on\"";

} %>
<input type="checkbox" name="no_del_file" <%=checked%>>

It returned on when checked 检查后返回

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

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