2011-10-28 48 views
3

我正在使用GWT 2.4。我试图提交一个AJAX请求,唯一的输入是页面上文本字段的值。这里是我如何将处理程序附加到页面的按钮...GWT:无法从文本框中获取价值

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的文本字段的最新值?

谢谢 - 戴夫

回答

2

尝试使用DOM.getPropertyString/DOM.getElementProperty

下面是GWT源的getAttribute功能的Javadoc。很明显,对于少数浏览器来说,对javascript的“getAttribute”函数的支持可能不一致,因此应该使用Element和子类。

或者您可以使用DOM.getPropertyString来获取它使用的JavaScript对象符号中的值来获得TE的电流值

/** 
* 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没有。这里是小提琴

http://jsfiddle.net/GvNu4/

+0

谢谢你。 DOM.getElementProperty确实解决了这个问题。 – Dave

+0

在GWT2.6中不推荐使用此方法是否有可用于获取DOM元素值的新方法。 – Jess

0

那么像你说的这是一个AJAX请求所以你有任何代码... GWT的代码将继续运行。

您应该使用请求的回调并检查当时nameFieldElement的值。