使用Prototype的一点心得体会(四)

使用Field对象操作表单域 ( API: http://www.prototypejs.org/api/form )

示例代码:

< html >
< head >
< script  src ="prototype-1.6.0.2.js"  type ="text/javascript" ></ script >
</ head >
< body >
< input  id ="text1"  type ="text"   />< br >
< input  id ="text2"  type ="text"   />< br >
< input  id ="text3"  type ="text"   />< br >
< input  id ="text4"  type ="text"  value ="xxxxxxxxx" />< br >
< select  size ="3"  id ="st1" >
    
< option > Java </ option >
    
< option > Struts </ option >
    
< option > Spring </ option >
</ select >
< br >
< input  type ="button"  value ="aa"  onClick ="Field.clear('text1','text2','text3')" />< br >
< input  type ="button"  value ="bb"  onClick ="alert(Field.present('text1','text2','text3'))" />< br >
< input  type ="button"  value ="cc"  onClick ="Field.focus('text3')" />< br >
< input  type ="button"  value ="dd"  onClick ="Field.select('text4')" />< br >
< input  type ="button"  value ="ee"  onClick ="Field.activate('st1')" />< br >
</ body >
< script >
</ script >

函数介绍:

1.clear : 清除传入该方法的所有表单域得值。

2.present : 判断所有表单域是否都有值。

3.focus : 指定焦点。

4.select : 选中表单域内的文本(只对文本框,文本有效)。

5.activate : 同select,并额外多一个功能:如果目标元素是不可选中的,则焦点移动到目标元素。

此外,还可以使用Form.Element对象,示例代码如下:

< html >
< head >
< script  src ="prototype-1.6.0.2.js"  type ="text/javascript" ></ script >
</ head >
< body >
< input  id ="text1"  name ="text1"  type ="text"   />< br >
< input  type ="button"  value ="aaa"  onClick ="alert(Form.Element.serialize('text1'));" />< br >
< input  type ="button"  value ="bbb"  onClick ="alert(Form.Element.getValue('text1'));" />< br >
</ body >
</ html >

你可能感兴趣的:(使用Prototype的一点心得体会(四))