DWRUtil.getText(id)
可以根據id取得text的值,這個方法只能用於select list
DWRUtil.getValue(id)
可以根據id取得value,使用這個方法,你不必在意div和select list的不同.
DWRUtil.getValues()
getValues() is similar to getValue() except that the input is a Javascript object that contains name/value pairs. The names are assumed to be the IDs of HTML elements, and the values are altered to reflect the contents of those IDs. This method does not return the object in question, it alters the value that you pass to it.
這個方法和getValue()一樣,只是它傳入的是一個包含名字和數值的j avascript對象.這個名字就是HTML元素的ID.這個方法不會返回任何對象,它只會將ID的value映射給傳入的value.例:
function doGetValues() {
var text= "{
div:null,
textarea:null,
select:null,
text:null,
password:null,
formbutton:null,
button:null
}";
var object = objectEval(text); //j avascript對象
DWRUtil.getValues(object);
var reply = DWRUtil.toDescriptiveString(object, 2); //toString
reply = reply.replace(/n/g, "<br/>"); //轉意
DWRUtil.setValue("getvaluesret", reply); //顯示
}
DWRUtil.setValue(id, value)
用ID找到元素,並更新value
DWRUtil.setValues()
和setValue(id,value)一樣,只是它需要的參數是個j avascript對象,如:
DWRUtil.setValues({
div: "new div content",
password: "1234567890"
});
DWRUtil.toDescriptiveString
帶debug資訊的toString,第一個為將要debug的對象,第二個參數為處理等級.等級如下:
0: Single line of debug 單行調試
1: Multi-line debug that does not dig into child objects 不分析子項目的多行調試
2: Multi-line debug that digs into the 2nd layer of child objects 最多分析到第二層子項目的多行調試
And so on. Level 2 and greater probably produce too much output.
總結:DWR不但屏蔽了許多client與server互動的重複且複雜的代碼,而且還提供了一些常用的方法,一些思想還是從prototype繼承而來,並有一定的改進.同時,它也考慮到了與struts、hibernate、spring的結合問題.
需要注意的是,DWR是一種把伺服器端的java代碼通過j avascript直接從瀏覽器調用的方法(DWR is a way of calling Java code on the server directly from Javascript in the browser.),而不是一個j avascript的庫(Generally speaking DWR is not a generic JavaScript library so it does not attempt to provide fill this need. However this is one of these really useful functions to have around if you are doing Ajax work.)能做到怎麼多,已經很難得了.
DWR自04年11月草案提出到現在的Version 1.1 beta 3(2005-12-29),已經更新發布了20多次了,但願在ajax的發展大路上,能始終看見DWR的身影.