提交之前添加POST参数 [英] Adding POST parameters before submit

查看:112
本文介绍了提交之前添加POST参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 < form id =commentFormmethod =POSTaction = API /注释> 
< input type =textname =nametitle =Your name/>
< textarea cols =40rows =10name =commenttitle =输入评论>
< / textarea>
< input type =submitvalue =发布/>
< input type =resetvalue =Reset/>
< / form>

我需要在发送到服务器之前添加两个POST参数:

  var params = [
{
名称:url,
值:window.location.pathname
},
{
name:time,
value:new Date()。getTime()
}
];

不需要修改表格。

解决方案

使用Jquery添加:

  $('#commentForm')。submit (函数(){//监听提交事件
$ .each(params,function(i,param){
$('< input />').attr('type', 'hidden')
.attr('name',param.name)
.attr('value',param.value)
.appendTo('#commentForm');
});

返回true;
});


I've this simple form:

<form id="commentForm" method="POST" action="api/comment">
    <input type="text" name="name" title="Your name"/>
    <textarea  cols="40" rows="10" name="comment" title="Enter a comment">
    </textarea>
    <input type="submit" value="Post"/>
    <input type="reset" value="Reset"/>
</form>

I need to add two POST parameters before send to the server:

var params = [
               {
                 name: "url",
                 value: window.location.pathname
               },
               {
                  name: "time",
                  value: new Date().getTime()
               }
             ];

without modifying the form, please.

解决方案

To add that using Jquery:

$('#commentForm').submit(function(){ //listen for submit event
    $.each(params, function(i,param){
        $('<input />').attr('type', 'hidden')
            .attr('name', param.name)
            .attr('value', param.value)
            .appendTo('#commentForm');
    });

    return true;
}); 

这篇关于提交之前添加POST参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆