在JS中模擬表單的post提交,進行頁面的跳轉
阿新 • • 發佈:2018-12-16
封裝為Post(URL, PARAMTERS) 函式:
/*
*功能: 模擬form表單的提交
*引數: URL 跳轉地址 PARAMTERS 引數
*/
function Post(URL, PARAMTERS) {
//建立form表單
var temp_form = document.createElement("form");
temp_form.action = URL;
//如需開啟新視窗,form的target屬性要設定為'_blank'
temp_form.target = "_self" ;
temp_form.method = "post";
temp_form.style.display = "none";
//新增引數
for (var item in PARAMTERS) {
var opt = document.createElement("textarea");
opt.name = PARAMTERS[item].name;
opt.value = PARAMTERS[item].value;
temp_form. appendChild(opt);
}
document.body.appendChild(temp_form);
//提交資料
temp_form.submit();
}
呼叫模組:
params = {
'WIDsubject':'競賽報名',
'money':money,
'body':cid
};
Post("alipay/index.php",params);