2011-10-28 18 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"); 

誰かが、自分のページIDを与えられたテキストフィールドの最新の値を返すために私のハンドラの中でGWTコードを書く方法を知っていますか?

おかげで、 - Daveはまあ、あなたのような

答えて

2

DOM.getPropertyString/DOM.getElementProperty

を使用してみてはのgetAttribute機能のためのGWTソースからのjavadocです。明らかに、javascriptの「getAttribute」関数のサポートは、いくつかのブラウザでは一貫性がないため、Elementとサブクラスを使用する必要があります。

またあなたは、私がIE8で、テキストフィールドの値を取得するには、JavaScriptの者「のgetAttribute」機能を使用してみました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のオブジェクト表記法を使用して値を取得するためにDOM.getPropertyStringを使用することができますおよびFF6。 IEはテキストフィールドの更新された値を返し、FFは返さなかった。ここではフィドルそのため

http://jsfiddle.net/GvNu4/

+0

おかげです。 DOM.getElementPropertyは実際に問題を解決しました。 – Dave

+0

このメソッドはGWT2.6では非推奨です.DOM要素の値を取得するための新しいメソッドがあります。 – Jess

0

は、それはあなたが持っているものは何でもコード... GWTのコードを実行し続けますので、AJAX要求だと述べました。

リクエストのコールバックを使用し、その時点でnameFieldElementの値をチェックする必要があります。

関連する問題