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設定されます。は、「3」ではなく「オン」の文字列を持つメッセージを表示します。spaninputWindow.alert(checkBox.getFormValue())

4

2 に答える 2

3

プロパティ「value=on / off」には、チェックボックスに対して事前定義された意味があります。したがって、数値を格納するためにプロパティ「value」を使用しないでください。

本当に数値を保存したい場合は、次のような独自のカスタムプロパティを使用してください-

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 に答える