autoFill.js
(function(window,document,undefined){
//定义自动填充表单工具
var autoFill = {
//定义表单元素常量
_TEXT:0,
_PASSWORD:1,
_SELECT:2,
_RADIO:3,
_LABLE:4,
//自动填充表单的数据[{field:userName,value:william,type:text}]
_formData:[],
_formDataPath:[],
_formDataIndexPath:"formData/index.js",
_formDataIndex:{},
//初始化方法
init: function(options){
_formDataPath = options.formDataPath;
},
// 填充HTML元素
_inputData: function(){
var self = this;
self._formDataPath = self._formDataIndex[$(document).find('#test0').val()];
$.ajax({
url: self._formDataPath,
type:'GET',
success:function(response){
self._formData = eval(response);
var len = self._formData.length;
if(len==0){return}
for(var i=0;i<len;i++){
var tmp = self._formData[i];
var element = $(tmp.field);
var type = self._getType(element);
switch (type){
case self._TEXT:
element.val(tmp.value);
break;
case self._PASSWORD:
element.val(tmp.value);
break;
case self._SELECT:
element.val(tmp.value);
break;
case self._RADIO:
break;
case self._LABLE:
element.text(tmp.value);
break;
}
}
}
});
},
// 判断填充的HTML元素类型
_getType: function(element){
var self = this;
var type = element.attr('type');
if(/text/.test(type)){
return self._TEXT;
}
if(/password/.test(type)){
return self._PASSWORD;
}
if($(element).is('select')){
return self._SELECT;
}
if(/radio/.test(type)){
return self._RADIO;
}
if($(element).is('span')){
return self._LABLE;
}
}
}
// 监听F11按键,并执行表单填充功能
$(document).keydown(function(e){
switch(e.keyCode){
case 122 :
$.ajax({
url:autoFill._formDataIndexPath,
type:'GET',
success:function(response){
autoFill._formDataIndex = (eval(response))[0];
autoFill._inputData();
}
});
return false;
}
})
})(window,document);
index.js
[{
loginFill:'formData/loginFill.js'
}]
loginFill.js
[
{field:'#loginUsername',value:'test'},
{field:'#loginPassword',value:'test'}
]