2013-03-05 87 views
1

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

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'。

+0

你说的设置值平均(除开/关)复选框?你想在复选框旁边添加一个标签吗? – 2013-03-05 12:28:43

+0

用户可以看到标签为'Some label'的复选框。但是当复选框被选中并且表单被提交时,发送表示复选框的其他值,例如'3',但没有'某个标签'。 – LancerX 2013-03-05 12:33:09

+0

你在使用FormPanel吗? – SSR 2013-03-05 14:07:30

回答

2

集的checkBox"someValue"使用setFormValue

checkBox.setFormValue("someValue"); 
+0

为什么要从GWT 1.6文档推送旧链接? – SSR 2013-03-05 14:01:53

+0

对不起,再来? – 2013-03-05 14:05:48

+0

编辑后指向GWT最新的文档,而不是GWT 1.6 – SSR 2013-03-05 14:08:24

3

的属性的值“的值=开/关”具有用于复选框一些预定义的含义。所以不要使用属性“值”来存储数字。

如果你真的要存储号码使用自己定制的属性,如 -

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

,并访问属性使用 -

checkBox.getElement().getFirstChildElement().getAttribute("customProperty");