【问题标题】:Programmatically sending data to iframe以编程方式将数据发送到 iframe
【发布时间】: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


【解决方案1】:

我通常在我想要验证的字段中添加一个类,同样的规则可以应用于你想要做的事情..

<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>

【讨论】:

  • 因为我的输入已经有类,它是否也可以像这样工作:class="req test" 然后在 .req 上做一个 foreach
  • @LiamSorsby 这对你有用吗?如果是,请您接受答案 - 如果不是,请提供反馈,我会尽力为您提供进一步的帮助
  • 抱歉,丹尼,正在玩它。似乎不想将数据发布到 iframe,所以还不安静。我已经编辑了我的帖子
  • 可能是因为创建的代码中没有指定名称吗?如果有的话,关于如何在输入字段上生成名称有什么建议吗?
  • 你如何将它发布到 iframe?
【解决方案2】:

此代码:form.attr("input:text",$(element).val()); 正在为每个具有 inputfield 类的元素设置/覆盖 &lt;form&gt; 标记中的属性,因此当您执行 form.submit(); 时,实际上将是这样的:

<form class="myform" input:text="valueOfLastElementWithClass'inputfield'">

我怀疑这是您想要实现的目标。我不确定您想要实现什么,但您可以在 jquery 中选择每个 &lt;input type="text"textareaselect,如下所示:

$('input[type=text],textarea,select').each(function(index, element) {
  // do something;
}

【讨论】:

  • 谢谢,我会试一试,但它不仅仅是文本,它还会是 textarea 并选择。
  • 将其更改为还选择textareas 和selects,但是我看不出您想对它们做什么。他们不是已经在您的&lt;form&gt;&lt;/form&gt; 中了吗?
  • 对不起,我会进一步解释。这样做的目的是我的 Ajax 上传工作正常,但是它在 ie 9 或更低版本中不起作用,因为我使用的是 FormData,所以我需要将所有表单传递到 iframe 以传递到我的 php 脚本以避免必须更改我的添加到购物车脚本。
  • 您能张贴(部分)您的表格吗?您不需要将表单元素单独发送到 iframe。只是submit(),而target 已设置为postiframe,就像现有脚本已经做的那样。整个表单 - 文件、输入、texareas 和选择 - 将被发送到 iframe
  • 表格很大,但有些表格在上面。
猜你喜欢
  • 1970-01-01
  • 2019-06-15
  • 2013-06-10
  • 2011-06-12
  • 1970-01-01
  • 2012-04-21
  • 2023-03-10
  • 1970-01-01
  • 2011-11-03
相关资源
最近更新 更多