2011-10-31 65 views
0

所以我一直在研究這一切 - 我敢肯定代碼是正確的,但要解釋我試圖在動態生成的輸入中包含一個表,所以需要tr/td在那裏。我已經檢查了html/wrap/append等函數無濟於事...... :-(jquery wrap(動態元素)

查看評論那裏的完整腳本,但真的這是它的肉,任何幫助將不勝感激

我嘗試了包和文件寫入,但它不喜歡它。我缺少什麼???

$(document).ready(function() { 
    $('#btnAdd').click(function() { 
     var num  = $('.clonedInput').length; // how many "duplicatable" input  fields we currently have 
     var newNum = new Number(num + 1);  // the numeric ID of the new input  field being added 
     //http://charlie.griefer.com/blog/2009/09/17/jquery-dynamically-adding- form-elements/ 
     // create the new element via clone(), and manipulate it's ID using newNum  value 
     var newElem = $('#input' + num).clone().attr('id', 'input' +  newNum).wrap('<td />'); 
     // manipulate the name/id values of the input inside the new element 
     newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name'  + newNum).val(''); 
     newElem.children(':second').attr('id', 'amt' + newNum).attr('name', 'amt'  + newNum).val(''); 
     newElem.children(':third').attr('id', 'value' + newNum).attr('name',  'value' + newNum).val(''); 
     newElem.children(':fourth').attr('id', 'test' + newNum).attr('name',  'test' + newNum).val(''); 
     // insert the new element after the last "duplicatable" input field 
     document.write("<tr>"); 
     $('#input' + num).after(newElem); 
     document.write("</tr>"); 
+0

你能澄清你的問題是什麼嗎? –

+0

看一下'prepend()''append()'上的jQuery API - 我認爲這比'document.write()'更舒服。 – Smamatti

回答

0

$(document).ready()使用document.write()會消滅你現有的文檔,並開始一個新的文檔,我寧願懷疑這是你的意圖(特別是因爲你似乎試圖將行添加到現有的表)

您需要使用jQuery函數向DOM中插入新元素,而不是使用document.write()。有很多用於操作DOM的jQuery調用。你可以看到其中的一些here。我猜你想要電話如append()after()