当前位置 : 主页 > 网页制作 > JQuery >

jquery 模拟 form表单 提交

来源:互联网 收集:自由互联 发布时间:2021-06-15
1: function Post(URL, PARAMTERS) { //创建form表单 var temp_form = document.createElement("form"); temp_form.action = URL; //如需打开新窗口,form的target属性要设置为‘_blank‘ temp_form.target = "_self"; temp_form.meth

1:

function Post(URL, PARAMTERS) {
        //创建form表单
        var temp_form = document.createElement("form");
        temp_form.action = URL;
        //如需打开新窗口,form的target属性要设置为‘_blank‘
        temp_form.target = "_self";
        temp_form.method = "post";
        temp_form.style.display = "none";
        //添加参数  //此为提交多个
        for (var item in PARAMTERS) {
            var opt = document.createElement("input");
            //设置 name 参数
            opt.name = item;
            opt.value = PARAMTERS[item];
            temp_form.appendChild(opt);
        }
        //提交数据
        temp_form.submit();
    }

2:本页面跳转

//jquery 模拟表单提交 2
    function postCurrent(url,params){
        var form = $("<form method=‘post‘></form>");
        var input;
        form.attr({"action":url});
        $.each(params,function (key,value) {
            input = $("<input type=‘hidden‘>");
            input.attr({"name":key});
            input.val(value);
            form.append(input);
        });
        $(document.body).append(form);
        form.submit();
    }

3:打开新窗口

/**
 * form表单提交新打开页面
 * @param url
 * @param params
 */
function postNew(url,params,windowname){
    var form = $("<form method=‘post‘></form>");
    var input;
    form.attr({"action":url});
    $.each(params,function (key,value) {
        input = $("<input type=‘hidden‘>");
        input.attr({"name":key});
        input.val(value);
        form.append(input);
    });
    form.attr({"target":windowname});
    window.addEventListener("onsubmit",function(){ openWindow(windowname); });
    $(document.body).append(form);
    window.dispatchEvent(new Event("onsubmit"))
    form.submit();
}

function openWindow(name)
{
    window.open(‘about:blank‘,name,‘height=400, width=400, top=0, left=0, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, status=yes‘);
}

 

调用:

function CurlDownload(img) {
        var url = ‘/admin/pic_D‘;
        params = { ‘img‘:img, ‘_token‘:‘****‘ };
        postCurrent(url, params);
    }
网友评论