GWT:从文本框中获取值时遇到问题
我正在使用GWT2.4。我试图提交一个AJAX请求,唯一的输入是页面上文本字段的值。下面是我如何将处理程序附加到页面的按钮GWT:从文本框中获取值时遇到问题,gwt,Gwt,我正在使用GWT2.4。我试图提交一个AJAX请求,唯一的输入是页面上文本字段的值。下面是我如何将处理程序附加到页面的按钮 public void onModuleLoad() { ... final com.google.gwt.dom.client.Element submitElement = Document.get().getElementById(SUBMIT_BUTTON_ID); final Button submitButton = Button.wra
public void onModuleLoad() {
...
final com.google.gwt.dom.client.Element submitElement = Document.get().getElementById(SUBMIT_BUTTON_ID);
final Button submitButton = Button.wrap(submitElement);
...
// Add a handler to send the name to the server
GetHtmlHandler handler = new GetHtmlHandler();
submitButton.addClickHandler(handler);
}
但问题是。在我的处理程序中,每当我尝试获取文本字段的值时,它总是返回第一次加载页面时在文本字段中输入的值,而不是最新的值
class GetHtmlHandler implements ClickHandler {
/**
* Fired when the user clicks on the submitButton.
*/
public void onClick(ClickEvent event) {
submitRequest();
}
/**
* Send the name from the nameField to the server and wait for a
* response.
*/
private void submitRequest() {
...
final Element nameFieldElement = DOM.getElementById(Productplus_gwt.NAME_FIELD_ID);
// This always returns an old value.
String docId = nameFieldElement.getAttribute("value");
有人知道我如何在处理程序中编写GWT代码,以返回给定页面id的文本字段的最新值吗
谢谢,-Dave就像你说的,这是一个AJAX请求,所以不管你有什么代码。。。GWT代码将继续运行
此时应使用请求的回调并检查nameFieldElement的值。尝试使用DOM.getPropertyString/DOM.getElementProperty 下面是来自GWT源代码的用于getAttribute函数的javadoc。它清楚地表明,对javascript的“getAttribute”函数的支持在一些浏览器中可能不一致,因此应该使用元素和子类 或者,您可以使用DOM.getPropertyString获取一个值,该值使用javascript的对象表示法获取当前值
/**
* Retrieves an attribute value by name. Attribute support can be
* inconsistent across various browsers. Consider using the accessors in
* {@link Element} and its specific subclasses to retrieve attributes and
* properties.
*
* @param name The name of the attribute to retrieve
* @return The Attr value as a string, or the empty string if that attribute
* does not have a specified or default value
*/
public final String getAttribute(String name) {
return DOMImpl.impl.getAttribute(this, name);
}
我尝试使用javascript的“getAttribute”函数来获取IE8和FF6中文本字段的值。IE给出了文本字段的更新值,而FF没有给出。这是小提琴
谢谢你。DOM.getElementProperty确实解决了这个问题。GWT2.6中不推荐使用此方法。是否有新方法可用于获取DOM元素的值。