javascript - 以编程方式将数据发送到 iframe

标签 javascript jquery iframe form-data

我目前正在添加备份 jquery 函数来替换 FormData 以支持 i.e 9 及以下版本。然而我遇到了这篇文章: jQuery iframe file upload

我可以看到这是用于上传我需要的文件,但我的表单中有很多字段。有没有一种方法可以在不使用 formData 的情况下以编程方式获取表单中的所有文本字段。

我已经尝试过使用它,但它将获取所有输入字段,但没有文本区域字段,并且它将包含图像。

$("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=""/>

表单的其余大部分是根据之前选择的选项生成的,因此很难发布该代码

最佳答案

我通常会向我想要验证的字段添加一个类,相同的规则可以应用于您想要执行的操作..

<input type="text" class="req" name="forename" value="Forename.." />
<textarea name="comments" class="req">Comments..</textarea>

jQuery

$(".inputfield").each(function(index,element){
    form.append(element);
    //form.attr("input:text",$(element).val());
});

示例

HTML

<form action="scripts/addtocart.php" target="post_requests" enctype="multipart/form-data" type="post">
<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=""/>
    <input type="submit" />
</form>
<iframe name="post_requests" style="display:none;"></iframe>

<div id="response">
    Waiting for form to be posted..
</div>

Javascript

function postResponse(_html)
{
    document.getElementById('response').innerHTML = _html;
}

脚本/addcart.php

<script type="text/javascript">
var _html = 'The form was posted..';

parent.postResponse(_html);
</script>

关于javascript - 以编程方式将数据发送到 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18486207/

相关文章:

jquery - 我如何使用 jQuery 识别被点击的元素?

javascript - iframe.src 属性中的 "javascript:"、 "#"和 "javascript:false"有什么区别

javascript - 在 <a 标签点击的 div 中加载 javascript

javascript - 如何在还剩 0 秒时隐藏计时器

Javascript - 事件方法中的引用对象

javascript - 使多个 iframe 像普通窗口一样拖动、调整大小和最小化

javascript - 使用 iframe src 覆盖 alert()

javascript - 使用javascript在mvc路由的一部分中有效增加数字

javascript - 使用 jquery 设置不透明度

javascript - 让微调器在 Grails 中正常工作