如何在java中动态禁用复选框

时间:2012-09-07 06:57:36

标签: java html

我正在使用java动态构建html表,我想根据某些条件自动检查复选框,但是这样的情况是选中复选框,

下面是我使用的代码:

 if(isactive.equalsIgnoreCase("Y")){
                         checked="checked";
                     }
                     else{
                         checked="false";
                     }

和复选框代码:

htmlTable.append("<td align=\"left\" ><input type=\"checkbox\" name=\"headername_"+loopvariable+"\" id=\"headername_"+loopvariable+"\" value="+id+" checked="+checked+">"+columnname.toUpperCase()+"<br></td>");

if the value is not `Y` than uncheck the checkbox is my requirement but it is not working.

请告知如何去做。

此致

1 个答案:

答案 0 :(得分:1)

设置checked属性将产生一个选中的复选框,无论您将其设置为什么。

您需要在变量中包含整个属性:

if(isactive.equalsIgnoreCase("Y")){
    checked = "checked=\"checked\"";
} else {
    checked = "";
}

并相应地更新字符串连接:

"... value="+id+" " + checked + ">"