2012-03-05 7 views
0

ユーザーがn行を挿入できるので、テーブル行を複製してidを持つ配列を生成しようとしています。私が直面している問題は、そのテーブル行に1つの選択ドロップダウンオプションがあることです。どのように1つの単一の行にある他の入力タグと一緒に選択を複製するには? (このコードは、一度に行の2セット、2 appendTo(のだって)さんを生成し、助けてくれてありがとうを他の入力タグと一緒にクローンを選択しますか?

$("#add").click(function() { 
    $("#comTable tr:eq(0)").clone().find("input").each(function() { 
     // creates array of ids if the user wants to add more than 1 row so it is N[1], etc 
     $(this).val('').attr('id', function(_, id) { 
      return id + 'N' + '[' + i + ']' 
     }); 
     $(this).val('').attr('name', function(_, name) { 
      return name + 'N' + '[' + i + ']' 
     }); 
    }).end().appendTo("#comTable"); 

    $("#comTable tr:eq(0)").clone().find("select").each(function() { 
     // creates array of ids if the user wants to add more than 1 row so it is N[1], etc 
     $(this).val('').attr('id', function(_, id) { 
      return id + 'N' + '[' + i + ']' 
     }); 
     $(this).val('').attr('name', function(_, name) { 
      return name + 'N' + '[' + i + ']' 
     }); 
    }).end().appendTo("#comTable"); 
    i++; 
});​ 

答えて

1

あなただけinputなどのようなselect要素を選択することができます。!

旧コード:

$("#comTable tr:eq(0)").clone().find("input").each(function() { 
    // creates array of ids if the user wants to add more than 1 row so it is N[1], etc 
    $(this).val('').attr('id', function(_, id) { 
     return id + 'N' + '[' + i + ']' 
    }); 
    $(this).val('').attr('name', function(_, name) { 
     return name + 'N' + '[' + i + ']' 
    }); 
}).end().appendTo("#comTable"); 

$("#comTable tr:eq(0)").clone().find("select").each(function() { 
    // creates array of ids if the user wants to add more than 1 row so it is N[1], etc 
    $(this).val('').attr('id', function(_, id) { 
     return id + 'N' + '[' + i + ']' 
    }); 
    $(this).val('').attr('name', function(_, name) { 
     return name + 'N' + '[' + i + ']' 
    }); 
}).end().appendTo("#comTable"); 

新しいコード:

$("#comTable tr:eq(0)").clone().find("input, select").each(function() { 
    // creates array of ids if the user wants to add more than 1 row so it is N[1], etc 
    $(this).val('').attr('id', function(_, id) { 
     return id + 'N' + '[' + i + ']' 
    }); 
    $(this).val('').attr('name', function(_, name) { 
     return name + 'N' + '[' + i + ']' 
    }); 
}).end().appendTo("#comTable"); 

ノート"input, select"セレクタ。異なっ

編集

あなたがselect Sを処理したい場合は、それを行うことができ、他の方法が異なっているチェーンに:

$("#comTable tr:eq(0)") 
    .clone() 
    .find("input") 
    .each(function() { 
      // creates array of ids if the user wants to add more than 1 row so it is N[1], etc 
      $(this).val('').attr('id', function(_, id) { 
       return id + 'N' + '[' + i + ']' 
      }); 
      $(this).val('').attr('name', function(_, name) { 
       return name + 'N' + '[' + i + ']' 
      }); 
     }) 
    .end() //End .find("input") 
    .find("select") 
    .each(function() { 
      // creates array of ids if the user wants to add more than 1 row so it is N[1], etc 
      $(this).val('').attr('id', function(_, id) { 
       return id + 'N' + '[' + i + ']' 
      }); 
      $(this).val('').attr('name', function(_, name) { 
       return name + 'N' + '[' + i + ']' 
      }); 
     }) 
    .end() //End .find("select") 
    .appendTo("#comTable"); 
i++; 

この方法では、余分なクローンを削除し、再び.findを実行します新しく複製されたDOM要素に追加します。

+0

ありがとうございました!私は.find()の方法を試していたが、最新のEDITが最善の方法と思われる! – Nikhil

+0

しかしこれはIEではうまくいきません!任意の提案.. – Nikhil

+0

@ニクヒル - それはエラーを投げているのですか? –

関連する問題