1

我想将值(数字或字符串)设置为复选框。这段代码

final CheckBox checkBox = new CheckBox("Some label");
checkBox.getElement().setAttribute("value", i.toString());
checkBox.getElement().getStyle().setProperty("color", colorList.get(i));
checkBox.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            Object sender = event.getSource();
            if (sender == checkBox) {
                CheckBox checkBox = (CheckBox)event.getSource();
                Window.alert(checkBox.getFormValue());
            }
        }
});

创建以下 HTML:

<td align="left" style="vertical-align: top;">
<span class="gwt-CheckBox" value="3" style="color: rgb(128, 105, 155);">
<input id="gwt-uid-4" type="checkbox" value="on" tabindex="0">
<label for="gwt-uid-4">Some label</label>
</span>
</td>

该属性value设置为span而不是inputWindow.alert(checkBox.getFormValue())显示带有字符串“on”而不是“3”的消息。

4

2 回答 2

3

属性“value= on / off”对复选框有一些预定义的含义。所以不要使用属性“值”来存储数字。

如果您真的想存储数字,请使用您自己的自定义属性,例如 -

checkBox.getElement().getFirstChildElement().setAttribute( "customProperty", "3" );

并访问属性使用 -

checkBox.getElement().getFirstChildElement().getAttribute( "customProperty" );
于 2013-03-05T13:28:46.073 回答
2

设置checkBox"someValue"使用的值setFormValue

  checkBox.setFormValue("someValue");
于 2013-03-05T12:42:09.780 回答