GWT-如何设置和获取Button()的value属性



我有一个Button元素,我想设置然后获取这个按钮的value属性。

当前渲染:

<button type="button" class="gwt-Button">page 1</button>

我想要实现的目标:

<button type="button" class="gwt-Button" value="1">page 1</button>

代码:

final Button b = new Button("page 1");
// HOW TO: set value for the button
b.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
        // HOW TO: get value for the button
    }
});

文档对此只字未提,这个SO问题的答案甚至没有编译("方法setValue(String)对于类型Button是未定义的")

我对此不能100%确定,但您可以尝试访问底层按钮元素本身,并从中修改/读取值。类似:

button.getElement().<ButtonElement>cast().setValue("1");

查看更多:

  • http://www.gwtproject.org/javadoc/latest/index.html
  • http://www.gwtproject.org/javadoc/latest/com/google/gwt/dom/client/ButtonElement.html

最新更新