【问题标题】:adding data to form right before ajax post在 ajax 发布之前将数据添加到表单
【发布时间】:2017-02-22 22:56:12
【问题描述】:

我正在尝试使用 beforeSend 函数在发布之前将一些数据添加到我的表单中,但数据没有通过帖子传递。我猜想在添加数据之前表单会被序列化,但这只是一个猜测。

这是我的 jquery/ajax:

$.ajax({
  type: "POST",
  url: '@Url.Action( "SaveHeaders", "Tally" )',
  //data: { model: @Html.Raw(Json.Encode(@Model)) },
  data: $('#myForm').serialize(),
  beforeSend: function() {
  var displayIndex = imageIndex+1;
  $("#images tbody").append("<tr><td class='text-center align-middle'>" + displayIndex + "<input type='hidden' id='SellerGroup_" + imageIndex + "__imageId' class='form-control text-box single-line' name='SellerGroup[" + imageIndex + "].imageId' readonly='readonly' value='" + $('#imageName').val() + "' /><td><input type='text' id='SellerGroup_" + imageIndex + "__majorGroup' class='form-control text-box single-line' name='SellerGroup[" + imageIndex + "].majorGroup' readonly='readonly' value='" + major + "' /></td><td><input type='text' id='SellerGroup_" + imageIndex + "__minorGroup' class='form-control text-box single-line' name='SellerGroup[" + imageIndex + "].minorGroup' readonly='readonly' value='" + minor + "' /></td></tr>");
                    },
                    success: function (data) {
                        console.log(data);
                    }
                });

【问题讨论】:

    标签: jquery ajax


    【解决方案1】:

    你运行$.ajax(...)代码之前,试着把这个:

    $('#myForm').append('<input type="hidden" name="whateverName" value="whateverValue" />');
    

    然后才运行您的代码(使用$('#myForm').serialize() 方法)。

    【讨论】:

    • 哈哈,你基本上是对的。我将 beforeSend 函数中的代码移到了 ajax 调用之上,现在它可以按预期工作了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    相关资源
    最近更新 更多