2011-10-28 88 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的值。