2012-03-05 27 views
0

我想克隆一個表格行,並生成一個ID爲數組,因爲用戶可以插入n行。我面臨的問題是,我在該表格行中有1個選擇下拉選項。如何克隆一個選擇以及其他單行的輸入標記? (此代碼生成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

你可以只選擇inputselect元素,像這樣:!

舊代碼:

$("#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()方式,但最新的編輯似乎是最好的方式! – Nikhil 2012-03-05 17:59:43

+0

但是這在IE中不起作用!任何建議.. – Nikhil 2012-03-05 18:04:14

+0

@Nikhil - 它是否引發錯誤? – 2012-03-05 19:11:11