js模拟form表单提交数据源码:
/**
* js模拟form表单提交
* @param {object} 参数对象
* url 必填 提交地址
* methond 选填 默认post 提交方式 post get
* target 选填 默认_self 当前页面还是新页面 _self _blank
* 其它参数
*/
function jsFormSubmit(params) {
var turnForm = document.createElement("form");
//一定要加入到body中!!
document.body.appendChild(turnForm);
var method = params['methond'] || "POST"; //默认为post
turnForm.method = method;
delete params['methond'];
var target = params['target'] || "_self"; //默认为当前页面
turnForm.target = target;
delete params['target'];
var url = params.url; //提交地址
turnForm.action = url;
delete params['url'];
//创建隐藏表单
for(var item in params){
var newElement = document.createElement("input");
newElement.setAttribute("type","hidden");
newElement.setAttribute("name",item);
newElement.setAttribute("value",params[item]);
turnForm.appendChild(newElement);
}
turnForm.submit();
}
js模拟form表单提交使用示例:
var params = {
//必填参数
"url":url,
"methond":"post",
"target":"_blank",
//下边为要提交的数据
"j_username":"SYNKMXS0000000043",
"j_password":"3258"
}
formSubmit(params);
js模拟a点击源码:
/**
* js模拟a点击
* @param {string} url 必填 提交地址
* @param {string} target 选填 默认_self 当前页面还是新页面 _self _blank
*/
function jsALink(url,target) {
var target = target || "_blank";
//先读取alink 不存在则创建
var alink = document.getElementById("alink");
if(alink==undefined) alink=document.createElement("a");
//赋值
alink.id = "alink";
alink.href = url;
alink.target = target;
alink.style = "display: none;";
//写入页面
document.body.appendChild(alink);
//执行点击事件
document.getElementById("alink").click();
}
js模拟a点击示例:
jsALink("http://www.jsfun.cn","_blank");
来源:jsfun.cn