a标签进行post请求

a标签加上js代码,进行post请求的写法:

<a href="javascript:doPost('{:url('index/operation/add')}', {'user_id':'111','active_id':'222','oper_time':'2018-10-3 15:57:05','oper':'$HHH'})">提交</a>
<!--<a href="javascript:doPost('{:url('index/operation/add')}', {'user_id':'111'})">提交</a>-->
<script>
    function doPost(to, p) {  // to:提交动作(action),p:参数
        var myForm = document.createElement("form");
        myForm.method = "post";
        myForm.action = to;
        for (var i in p){
            var myInput = document.createElement("input");
            myInput.setAttribute("name", i);  // 为input对象设置name
            myInput.setAttribute("value", p[i]);  // 为input对象设置value
            myForm.appendChild(myInput);
        }
        document.body.appendChild(myForm);
        myForm.submit();
        document.body.removeChild(myForm);  // 提交后移除创建的form
    }
</script>
本站原创,未经允许不得转载:lingkb » a标签进行post请求
赞(0)

评论抢沙发