【问题标题】:my jquery-built form is submitting, but not sending any of the values!我的 jquery 构建的表单正在提交,但没有发送任何值!
【发布时间】:2011-01-28 15:30:05
【问题描述】:

我正在尝试执行类似于此处找到的插件的操作: http://www.filamentgroup.com/lab/jquery_plugin_for_requesting_ajax_like_file_downloads/ 确实如此。我原本打算使用它,但它在日期方面遇到了很多麻烦。所以我想我会尝试改变一下......

这是我的插件版本:

jQuery.download = function (url, data, method) {
if (url && typeof data == 'object') {
    //for this version, data needs to be a json object.  
    //loop through the data object..

    var theForm = $('<form></form>').attr('action', url).attr('method', method).attr('id', 'jqueryDownloadForm');

    $.each(data, function (propertyName, propertyVal) {

        theForm.append($("<input />").attr('type', 'hidden').attr('id', propertyName).val(propertyVal));
    });

    theForm.appendTo('body').trigger('submit').remove();
}
else {
    //they didn't fill in the params.  do nothing
}

};

它的名称如下:

var dataToPost = { reportFormatId: reportFormatId, reportPageSizeId: reportPageSizeId, reportSortOrderId: reportSortOrderId, rangeStart: rangeStart, rangeEnd: rangeEnd};
 $.download('/The/Url/', dataToPost, "POST");

它构建表单并提交。但它不发送任何输入。

如果我遍历 theForm.find('input') 它们都有值。但是用提琴手看帖子,没有发布任何内容。我的 mvc 控制器给了我空参数错误。

提前感谢您的帮助!

编辑: 我尝试在表单中添加一个按钮。像这样:

theForm.append($("<input />").attr('type', 'submit').attr('id', "ExportForm").val("Export"));

而不是从 JavaScript 提交。如果我点击按钮,它也不会发送任何数据。

【问题讨论】:

  • 你有没有试过在函数末尾加一个return false?
  • 提交后?没有帮助。

标签: javascript jquery forms post


【解决方案1】:

您生成的&lt;input&gt; 元素没有name 属性。由于该属性(不是id)用于在发布表单时识别控件,因此实际上没有任何内容提交到服务器。

尝试类似:

theForm.append($("<input />").attr("type", "hidden")
    .attr("name", propertyName).val(propertyVal));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-04
    • 2019-09-17
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2023-03-20
    相关资源
    最近更新 更多