Prototype两个惯用监听器
Prototype两个常用监听器

1.Form.Observer(form,interval,callback)
如果表单form内任何表单控件的值发生改变,interal秒后自动触发callback函数。该表单既可以是表单的id属性,也可以是表单本身。
<!DOCTYPE html> <html> <head> <meta name="author" content="OwenWilliam" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> 使用Form.Observer </title> </head> <body> <form id="test" method="post" action="#"> 用户名:<input type="text" id="user" name="user" /><br/> 密码:<input type="text" id="pass" name="pass" /> </form> <script src="../../prototype.js" type="text/javascript"> </script> <script type="text/javascript"> // 为test表单绑定事件监听器 new Form.Observer("test", 1, function() { // 此处的this.getValue()将返回目标表单的serialize() alert(this.getValue()); }); </script> </body> </html>
结果:
2.Form.Element.Observer(element,interval,callback)
如果表单控件element的值发生改变,interval秒后自动触发callback函数。该element既可以是表单控件的id属性,也可以是表单控件本身。
<!DOCTYPE html> <html> <head> <meta name="author" content="OwenWilliam" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> 使用Form.Element.Observer </title> </head> <body> 用户名:<input type="text" id="user" name="user" /> <script src="../../prototype.js" type="text/javascript"> </script> <script type="text/javascript"> // 为user表单控件绑定事件监听器 new Form.Element.Observer("user", 1, function() { // 此处的this.getValue()将返回目标表单的getValue() alert(this.getValue()); }); </script> </body> </html>
结果: