jquery模拟form表单post提交

jquery模拟form表单post提交

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
 * jquery模拟form表单post提交
 * Created by Steven Guo on 2016/3/22.
 */
define(function(require , exports , module) {

    var myPost = function(url,args){
        var body = $(document.body),
            form = $("<form method='post'></form>"),
            input;
        form.attr({"action":url});
        $.each(args,function(key,value){
            input = $("<input type='hidden'>");
            input.attr({"name":key});
            input.val(value);
            form.append(input);
        });

        form.appendTo(document.body);
        form.submit();
        document.body.removeChild(form[0]);
    }

    module.exports.myPost = myPost;
});