2

Jクエリ:

$('#partsOrderQty').blur(function(){

var rowCounter = $(this).val();

console.log(rowCounter);

var toAddRow = $("#skidListings").children().clone();

for (var i=0; i<rowCounter; i++) {

    $("#skidListings").append(toAddRow);

    console.log("Ran Once Just Now");

}

});

html:

<div id="skidListings">

        <input class="ConfirmQty" name="ConfirmQty" type="text" value="<?php echo $confirm;?>"/>

        <input class="ConfirmDesc" name="ConfirmDesc" type="text" value="<?php echo $des[$final]; ?>"/>

        <input class="ConfirmWeight" name="ConfirmWeight" type="text" value="<?php echo $ShipWeight[$final]*$confirm; ?>" />

        <input class="ConfirmClass" name="ConfirmClass" type="text" value="<?php echo $class[$final]; ?>"/>

        <input class="ConfirmNMFC" name="ConfirmNMFC" type="text"  value="<?php echo $NMFC[$final]; ?>"/>

        <input class="ConfirmLength" name="ConfirmLength" type="text" value="<?php echo $length[$final]; ?>"/>

        <input class="ConfirmWidth" name="ConfirmWidth" type="text" value="<?php echo $width[$final]; ?>" />

        <input class="ConfirmHeight" name="ConfirmHeight" type="text" value="<?php echo $height[$final]; ?>" /><br />   

    </div>

コンソール メッセージは正しい回数出力されるのに、なぜこの複製は 1 回しか行われないのですか? 値をクリアする必要がありますか? この機能は、データベースから取得したものとは異なる数のピースを手動で指定できるようにすることです。ID を使用してクローンを作成できないことは理解していますが、ID を持たない子を選択しました。クラスを保持する必要があります。名前を削除する必要がありますか? それは問題ですか?

編集:リクエストごとにhttp://jsfiddle.net/NxSwA/

4

2 に答える 2

2

これがあなたが探しているものだと思います... 1つの問題(少なくとも私はそれを問題として見ました)は、2を追加すると(予想どおり)3になることですが、1を追加すると4つある?代わりに、の内容全体を複製しているため、最終的には 6 になります。#skidListings

それぞれの skidListing を でラップしdiv class=skidListing、それらの 1 つのインスタンスを複製するだけです...おそらく最後のものは次のようになります。

http://jsfiddle.net/NxSwA/1/

$('#partsOrderQty').blur(function () {
    var rowCounter = $(this).val();
    console.log(rowCounter);
    var toAddRow = $("#skidListings").children(".skidListing").last();
    for (var i = 0; i < rowCounter; i++) {
        $("#skidListings").append(toAddRow.clone());
        console.log("Ran Once Just Now");
    }
});

HTML

<input type="text" id="partsOrderQty" />
<div id="skidListings">
    <div class="skidListing">
        <input class="ConfirmQty" name="ConfirmQty" type="text" value="<?php echo $confirm;?>" />
        <input class="ConfirmDesc" name="ConfirmDesc" type="text" value="<?php echo $des[$final]; ?>" />
        <input class="ConfirmWeight" name="ConfirmWeight" type="text" value="<?php echo $ShipWeight[$final]*$confirm; ?>" />
        <input class="ConfirmClass" name="ConfirmClass" type="text" value="<?php echo $class[$final]; ?>" />
        <input class="ConfirmNMFC" name="ConfirmNMFC" type="text" value="<?php echo $NMFC[$final]; ?>" />
        <input class="ConfirmLength" name="ConfirmLength" type="text" value="<?php echo $length[$final]; ?>" />
        <input class="ConfirmWidth" name="ConfirmWidth" type="text" value="<?php echo $width[$final]; ?>" />
        <input class="ConfirmHeight" name="ConfirmHeight" type="text" value="<?php echo $height[$final]; ?>" />
        <br />
    </div>
</div>

これに注意してください... 2 を足すと 3 になります... さらに 1 を足すと 4 になります (6 ではありません)。skidListingこれにより、CSS、JS でのデータの収集、一部の削除など、それぞれをより詳細に制御できるようになります。

于 2013-06-02T13:39:11.163 に答える
2

コードをに変更します

var toAddRow = $("#skidListings").children();
for (var i=0; i<rowCounter; i++) {
  $("#skidListings").append(toAddRow.clone());
  console.log("Ran Once Just Now");
}

これはうまくいくはずです

更新 #1: その理由は .append ドキュメントから次のとおりです以下のコメントのフェリックス・キング)

于 2013-06-02T13:20:32.760 に答える