【发布时间】:2013-08-28 11:08:24
【问题描述】:
我目前正在添加一个备份 jquery 函数来替换 FormData 以支持即 9 及以下版本。但是我遇到了这个帖子: jQuery iframe file upload
我可以看到这是用于上传我需要的文件,但我的表单中有很多字段。有没有一种方法可以在不使用 formData 的情况下以编程方式获取表单中的所有文本字段。
我看过使用它,但它会获取所有输入字段,但没有 textarea 字段,它会包含图像。
$("form").each(function(){
$(this).filter(':input') //<-- Should return all input elements in that specific form.
});
或者有更好的方法吗?我真的不想触摸 formData,因为它适用于所有其他浏览器,但我已经包含了代码
if(document.FormData === "undefined")
检测并使用另一个功能。
编辑:
刚刚尝试了以下方法:
$(".inputfield").each(function(index,element){
form.attr("input:text",$(element).val());
});
但它不会更新 iframe
编辑:
这是我用来上传表单的完整代码(希望如此)
if(window.FormData === undefined){
// create iframe
var iframe = $('<iframe name="postiframe" id="postiframe" style="display: none" />');
$("body").append(iframe);
var form = $('.myform');
form.attr("action", "scripts/addtocart.php");
form.attr("method", "post");
form.attr("enctype", "multipart/form-data");
form.attr("encoding", "multipart/form-data");
form.attr("target", "postiframe");
$(".inputfield").each(function(index,element){
form.attr("input:text",$(element).val());
});
form.submit();
$("#postiframe").load(function () {
iframeContents = $("#postiframe")[0].contentWindow.document.body.innerHTML;
$("#textarea").html(iframeContents);
});
alert("complete");
}
编辑:添加表格:
<form class="myform" onsubmit="return false;" enctype="multipart/form-data">
<input type="hidden" name="price" class="inputfield" value="<?php echo $price; ?>" />
<input type="hidden" class="inputfield" id="product" name="product" value="<?php echo $_GET['id']; ?>"/>
<input type="hidden" class="inputfield" id="sid" name="sid" value="<?php echo $_GET['sid']; ?>"/>
<input type="hidden" class="inputfield" id="hiddenvalue" name="label" value=""/>
<input type="hidden" class="inputfield" id="quantity" name="quantity" value="1"/>
<input type="hidden" class="inputfield" id="labelquantity" name="labelquantity" value=""/>
<input type="hidden" class="inputfield" id="userlabel" name="userlabel" value=""/>
<input type="hidden" class="inputfield" id="colourlabel" name="colourlabel" value=""/>
<input type="hidden" class="inputfield" id="foillabel" name="foillabel" value=""/>
表单的其余大部分是根据之前选择的选项生成的,因此很难发布该代码
【问题讨论】:
-
我没有看到你在这段代码中使用 iframe 做什么,可以解释或添加代码吗?还有一个简短的说明,不确定英语是否是你的第一语言,但你说“安静”几次,你的意思是“相当”。 “安静”的意思是“沉默”;)
-
@funkwurm 英语是我的第一语言,拼写当然不是我的强项,但感谢您指出它将被更新。我现在将更新问题和更多代码。
-
@funkwurm 我已经更新了
标签: javascript jquery iframe form-data