我使用以下方法在单击按钮时提交表单。我还需要将一个名为“selected”的数组附加到这篇文章。我不能为此使用 ajax,因为发布页面上会发生下载。我需要这个数组来处理要下载的文件。
我做了一些搜索并发现了这个,但也无法让它工作:
var input = $("<input>").attr({"type":"hidden","name":"selected"}).val(selected);
$('#test').append(input);
当前:
var selected = [1,2,54,56,23]; // could be anything
$('#test').submit(function(event) {
if (selected.length > 0)
{
$('#test').attr('action','/process/p_screenshots_download2.php');
$('#test').attr('type','post');
return;
}
event.preventDefault();
});
最佳答案
尝试
$('#test').submit(function(event) {
$.each(selected, function(i, v){
var input = $("<input>").attr({"type":"hidden","name":"selected[]"}).val(v);
$('#test').append(input);
});
if (selected.length > 0)
{
$('#test').prop('action','/process/p_screenshots_download2.php');
$('#test').prop('method','POST');
return;
}
event.preventDefault();
});
这将在提交之前将一组带有数组中的值的隐藏输入添加到表单中。
关于jquery - 将 jquery 数组附加到表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21209400/