发送FORM POST前删除参数

时间:2017-07-13 12:51:03

标签: javascript c# jquery html

我的页面上有一个用C#编写的表单,页面上有几个HTML元素。在按钮单击我使用jQuery并添加隐藏的字段比我提交表单到外部域。现在的问题是,它正在发送我不需要的所有参数。有什么办法可以只发送我需要的参数。我想只在体内发送参数。

在添加参数

之前是否可以清除表单参数

jQuery代码

$("#aspnetForm").attr("action", "www.example.com");
$("#aspnetForm").attr("method", "post");
var params = a.split('?')[1].split('&'); /*custom string with key/value and sperated with & */
$.each(params, function (index) {
    var paramsV = params[index].split('=');
    $("#aspnetForm").append('<input type="hidden" name="' + paramsV[0] + '" value="' + paramsV[1] + '" /> ');
});
$("#aspnetForm").submit();

在我的表单中动态添加了50多个HTML元素,并且在每个加载时它都有不同的id。所以我无法在Jquery或javascript中禁用这些元素

1 个答案:

答案 0 :(得分:1)

尽管给出了适当的解释,有些人仍然非常粗鲁。

我正在使用SharePoint 2013,它有自己的母版页,它添加了许多我无法控制的元素,因此我无法单独禁用它们。

我会感谢Krishna指出正确的方向。

他对ajax POST的回答无法解决,因为我收到了错误 No&#39; Access-Control-Allow-Origin&#39;标头出现在请求的资源上。起源&#39; 。我无法控制其他域名,所以我无法使用他的答案。

所以我做的是我动态创建添加必需参数然后提交它。

希望它可以帮助某人

$("#frm").remove(); /** remove extra frm before creating it **/
var frm = $('<form id="frm" action="' + _url + '" method="POST"></form>');
var params = a.split('?')[1].split('&'); /*custom string with key/value and sperated with & */
$.each(params, function (index) {
    var paramsV = params[index].split('=');
    frm.append('<input type="hidden" name="' + paramsV[0] + '" value="' + paramsV[1] + '" /> ');
});
frm.appendTo(document.body).submit();