访问HTML元素的值,只用getValue(elementID)方法
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>访问HTML元素的值,只用getValue(elementID)方法</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="js/util.js"></script> </head> <body> <input id="test" type="text" /> <input type="button" value="设置值" onclick="dwr.util.setValue('test' , '测试用的值');" /> <input type="button" value="获取值" onclick="alert(dwr.util.getValue('test'));" /> </body> </html>
访问HTML元素的值,只用getValues(obj)方法
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>访问HTML元素的值,只用getValues(obj)方法</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="js/util.js"></script> <script type="text/javascript"> function set() { //定义了一个JSON格式的对象,该对象的每个属性名 //对应页面中的一个HTML元素的ID属性值。 var obj = { txt : '测试文本', pass : '12345678', area : '学习Ajax\n很好玩', select : 'two' }; //调用该方法后,页面中对应的HTML元素值将被设置为上面JSON对象的属性值 dwr.util.setValues(obj); } function get() { //定义了一个JavaScript对象。 //该对象的属性名对应于页面中的HTML元素的id属性 var obj = { txt : null, pass: null, area: null, select: null}; //调用getValues方法后,obj的属性值将被设置为页面中对应HTML元素的值 dwr.util.getValues(obj); var result = ""; //通过遍历obj对象的属性,输出obj对象的所有属性值。 for (var name in obj) { result += obj[name] + "\n"; } alert(result); } </script> </head> <body> <input id="txt" type="text" /> <br /> <input id="pass" type="password" /> <br /> <textarea id="area" rows="2" cols="50"></textarea> <br /> <select id="select"> <option value="one"> 一 </option> <option value="two"> 二 </option> <option value="three"> 三 </option> </select> <input type="button" value="设置值" onclick="set();"/> <input type="button" value="获取值" onclick="get();" /> </body> </html>
util.js测试,dwr.util.onReturn(event,callbackfunction)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>util.js测试,dwr.util.onReturn(event,callbackfunction)</title> <meta name="website" content="http://www.crazyit.org" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script src="js/util.js" type="text/javascript"></script> <script type="text/javascript"> function test() { alert("单击了回车键"); } </script> </head> <body> <input id="txt" name="txt" type="text" onkeypress="dwr.util.onReturn(event,test)" /> </body> </html>
util.js测试dwr.util.toDescriptiveString(elementId,level)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>util.js测试</title> <meta name="website" content="http://www.crazyit.org" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script src="js/util.js" type="text/javascript"></script> </head> <body> <input id="test" type="text" value="0123456789" /> <input type="button" value="选中" onclick="dwr.util.selectRange('test',3,8)" /> </body> </html>