0

現在、9 以下をサポートするために FormData を置き換えるバックアップ jquery 関数を追加しています。しかし、私はこの投稿に出くわしました: 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=""/>

フォームの残りの大部分は、以前に選択したオプションに応じて生成されるため、そのコードを投稿するのは少し難しい

4

2 に答える 2

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>
于 2013-08-28T11:11:58.630 に答える