javascipt模拟生成form表单2种提交方式

<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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值