文章目录

JS - ExtendForm.js

JS

var Ly = {
        createElement : function(target, config){
        var target = target || 'div';
        var config = config || {};
        var tag = document.createElement(target);
            for(var p in config){
                if(p.toLowerCase() == 'style'){
                    tag.style.cssText = config[p];
                }else if(p.toLowerCase() == 'class' || p.toLowerCase() == 'cls'){
                    tag.className = config[p];
                }else if(p.toLowerCase() == 'innerhtml'){
                    tag.innerHtml = config[p];
                }else{
                    tag.setAttribute(p, config[p]);
                }
            }
            try{
                return tag;
            }finally{
                tag = null;
            }
        }
    };
var ExtendForm = {
            params : {},
            form : null,
            config : {},
            create : function () {
                document.body.appendChild(this.form = Ly.createElement('form', {
                    action : this.config.action,
                    method : this.config.method,
                    target : this.config.target
                }));
                return this;
            },
            init : function (config) {
                config = config || {};
                this.config = {
                    action : config.action || '?',
                    method : config.method || 'post',
                    target : config.target || '_self'
                };
                return this;
            },
            bind : function (data) {
                if(typeof data != 'object'){
                    return null;
                }
                this.params = {};
                for(var p in data){
                    switch (typeof data[p]){
                        case 'string':
                        case 'number':
                            this.params[p] = data[p];
                            break;
                        case 'object':
                            if(data[p].constructor === Array){
                                this.params[p] = data[p].join(',');
                            }
                            break;
                        case 'boolean':
                            this.params[p] = data[p];
                            break;
                    }
                }
                return this;
            },
            send :function () {
                if(!this.params){
                    return null;
                }
                this.create();
                for(var p in this.params){
                    this.form.appendChild(Ly.createElement('input', {
                        type : 'hidden',
                        name : p,
                        value : this.params[p]
                    }));
                }
                this.form.submit();
                this.clear();
                return this;
            },
            clear : function () {
                var inputs = this.form.childNodes;
                for(var i = inputs.length-1;i>-1;--i){
                    this.form.removeChild(inputs[i]);
                    inputs[i] && (inputs[i] = null);
                }
                document.body.removeChild(this.form);
                this.form = null;
            }
        };
ExtendForm.init({
        action : 'http://demo.xiaowiba.com/demo/result.php',
        target : '_blank',
        method : 'post'
    }).bind({
        param : '小尾巴'
    }).send();

DEMO

参数值:    *
发送方式: get    post
打开方式: 新页面    当前页
目标url: http://demo.xiaowiba.com/demo/result.php
返回文章列表 打赏
本页链接的二维码
打赏二维码
期待你的评论

6 条评论
  1. True 白银   True  

    灯泡很好玩,就是发不了评论啊@(haha)

    1. 小尾巴 王者   小尾巴   博主
      @ True

      目前还存在着不少bug

  2. jlqwer 白银   jlqwer  

    这个代码高亮的样式很棒;

    1. 小尾巴 王者   小尾巴   博主
      @ jlqwer

      哈哈哈~

  3. 亚克力浴缸 青铜   亚克力浴缸  

    这网站个性化好强啊,估计是个牛人。

    1. 小尾巴 王者   小尾巴   博主
      @ 亚克力浴缸

      低调低调~