javascript - 提交前添加POST参数

标签 javascript jquery forms

我有这个简单的表格:

<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>

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

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

请不要修改表格。

最佳答案

使用 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;
}); 

关于javascript - 提交前添加POST参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/993834/

相关文章:

javascript - AngularJS 获取变量中选定下拉选项的值

javascript - 为什么 didTransition 不调用我的 jquery?

javascript - 将 rateit jquery 插件应用于选择框

javascript - 如何检查 FormData 文件是否为空?

css - Chrome 中的输入按钮位置已关闭?

javascript - ajax请求服务器端脚本( Controller )来获取服务器的状态

javascript - 如何在 emberjs 中使用 Mirage 假数据进行分页?

javascript - 如何使用jquery一次隐藏多个值

javascript - 从另一个网站导入 <template> 元素

javascript - 如何获取具有数组名称的输入标签的检查值并将此值发送到服务器