0

我正在使用 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.

请告知如何去做。

问候

4

1 回答 1

1

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

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

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

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

"... value="+id+" " + checked + ">"
于 2012-09-07T07:04:04.460 回答