<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset="utf-8" />
<script src="jquery.1.4.2.js"></script>
<title></title>
</head>
<body>
<div>
<button id="clickGet">Get提交</button><br>
<button id="clickPost">Post提交</button>
</div>
<script>
//自定义 javascipt模拟生成form表单2种提交方式
(function(_window){
function getCurTime(){
return new Date().getTime();
}
var Form = function(){};
Form.submit = function(method, action, param){
if (param == undefined){
param = {};
}
if (method == undefined || action == undefined
|| !(Object.prototype.toString.call(method) === "[object String]")
|| !(Object.prototype.toString.call(action) === "[object String]")
|| !(Object.prototype.toString.call(param) === "[object Object]")){
console.log('param is error.');
return;
}
form = $('<form style="display:none"></form>').attr('name', 'form_'+getCurTime())
.attr('method', method)
.attr('action', action+'?_t='+getCurTime());
for ( name in param ) {
input = $('<input type="hidden" name="'+name+'"/>').attr('value', param[name]);
form.append(input);
}
form.appendTo('body');
form.submit();
}
_window.Form = Form;
})(window);
$(function(){
var url = window.location.href
url = url.split('?')[0];
function getCurTime(){
return new Date().getTime();
}
$('#clickGet').click(function(){
//方法一:简单方便
//window.location.href = url+'?_t='+getCurTime();
//方法二:复杂
//form = $('<form style="display:none" method="get"></form>').attr('name', 'form_'+getCurTime()).attr('action', url+'?_t='+getCurTime());
//input1 = $('<input type="hidden" name="orderStatus"/>').attr('value', -1);
//input2 = $('<input type="hidden" name="pageNo"/>').attr('value',2);
//form.append(input1).append(input2);
//form.appendTo('body');
//form.submit();
Form.submit('get', url, {'orderStatus':-1, 'pageNo':1});
});
$('#clickPost').click(function(){
//form = $('<form style="display:none" method="post"></form>').attr('name', 'form_'+getCurTime()).attr('action', url+'?_t='+getCurTime());
//input1 = $('<input type="hidden" name="orderStatus"/>').attr('value', -1);
//input2 = $('<input type="hidden" name="pageNo"/>').attr('value',2);
//form.append(input1).append(input2);
//form.appendTo('body');
//form.submit();
Form.submit('post', url, {'orderStatus':-1, 'pageNo':1});
});
});
</script>
</body>
</html>
javascipt模拟生成form表单2种提交方式
最新推荐文章于 2020-12-30 04:58:34 发布