在一个前端页面上,需要通过JavaScript来提交参数,使用JS创建form表单,将参数append到表单中进行提交,代码如下:
Js代码:
- function loadConfig(gameUrl,skinId){
- var temp = document.createElement("form");
- temp.action = "${createLink(controller:'mobileConfig', action:'beforeLaunchConfig')}";
- temp.method = "POST";
- temp.style.visibility="hidden";
- var opt = document.createElement("input");
- opt.name = "gameUrl";
- opt.id = "gameUrl";
- opt.value = gameUrl;
- var opt2 = document.createElement("input");
- opt2.name = "skinId";
- opt2.id = "skinId";
- opt2.value = skinId;
- temp.appendChild(opt);
- temp.appendChild(opt2);
- temp.submit();
- }
该功能在Chrome及Safari上都能成功运行,但在使用FireFox(17.0.1)时不能成功提交,经过研究发现,FireFox在提交页面表单时要求页面有完整的标签项,即<html><head><title></title></head><body><form></form</body</html>这样的标签结构。因此,将该段JS做了写小改动:
Js代码:
- function loadConfig(gameUrl,skinId){
- var pageDiv = document.getElementById("page");
- var temp = document.createElement("form");
- temp.action = "${createLink(controller:'mobileConfig', action:'beforeLaunchConfig')}";
- temp.method = "POST";
- temp.style.visibility="hidden";
- temp.name = "loadConfigPage";
- var opt = document.createElement("input");
- opt.name = "gameUrl";
- opt.id = "gameUrl";
- opt.value = gameUrl;
- var opt2 = document.createElement("input");
- opt2.name = "skinId";
- opt2.id = "skinId";
- opt2.value = skinId;
- temp.appendChild(opt);
- temp.appendChild(opt2);
- pageDiv.appendChild(temp);
- temp.submit();
- }
在<body>标签内append此处创建的form表单,再进行提交就能成功了。参考博文:http://bytes.com/topic/javascript/answers/542837-document-getelementbyid-myform-submit-does-not-work