2017-04-18 49 views
0

我有一個jQuery追加錶行的問題,我的情況是想追加新的行到表中的最後一行,追加的成功,但新行已附加到側表,如何修理它?我是新手使用這種方式。 jsfield中的示例代碼,謝謝。追加和刪除行表jquery

var i = 1; 
 
    $("#addbutton").click(function() { 
 
     $("#t tr:first").append('<tr>'+ 
 
      '<td>File</td>'+ 
 
      '<td> <input type="file" name="file[]" id="txtTitle" name="txtTitle"></td>'+ 
 
      '<td><button type="button" class="removebutton" title="Remove this row">X</button></td></tr>').find("input").each(function() { 
 
     }); 
 
     i++; 
 
    }); 
 

 
    $(document).on('click', 'button.removebutton', function() { 
 
     $(this).closest('tr').remove(); 
 
     return false; 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="t"> 
 
     <tr> 
 
      <td>Id Gor</td> 
 
      <td> <input type="text" name="Id Gor" id="txtTitle" name="txtTitle"></td> 
 
      <td>&nbsp;</td> 
 
     </tr> 
 
     <tr> 
 
      <td>File</td> 
 
      <td> <input type="file" name="file[]" id="txtTitle" name="txtTitle"></td> 
 
      <td>&nbsp;</td> 
 
     </tr> 
 
    </table> 
 
    <table> 
 
     <tr> 
 
      <td style="width: 33px;"></td> 
 
      <td style="width: 183px;">&nbsp;</td> 
 
      <td><input type="button" id="addbutton" value="Add Row"></td> 
 
     </tr> 
 
    </table>

enter code here 
+0

你要放置第一個文件的情況下輸入新的文件輸入?實際上你並沒有把它放在邊桌上,你把它放在同一張桌子上的新的一排,所以它從右側顯示。 – ReadyFreddy

+0

是的,我只是想刪除tr:首先在jquery中。感謝您的信息@ReadyFreddy – Zeus234

+0

歡迎您。你也可以通過打開谷歌開發控制檯來檢查這一點,並在你的腳本後自己查看html代碼。良好的編碼。 – ReadyFreddy

回答

3

的錯誤是,你正在使用$( 「#牛逼TR:第一」),該塊越來越表#T的第一行(),和那麼你正在做追加到它,行,所以實際上你的東西結束了像

<tr><tr></tr><tr></tr><tr></tr></tr> 

來完成你想要什麼,只是刪除TR:首先從選擇。下面是你的代碼與小修補工作。

var i = 1; 
 
    $("#addbutton").click(function() { 
 
     $("#t").append('<tr>'+ 
 
      '<td>File</td>'+ 
 
      '<td> <input type="file" name="file[]" id="txtTitle" name="txtTitle"></td>'+ 
 
      '<td><button type="button" class="removebutton" title="Remove this row">X</button></td></tr>').find("input").each(function() { 
 
     }); 
 
     i++; 
 
    }); 
 

 
    $(document).on('click', 'button.removebutton', function() { 
 
     $(this).closest('tr').remove(); 
 
     return false; 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="t"> 
 
     <tr> 
 
      <td>Id Gor</td> 
 
      <td> <input type="text" name="Id Gor" id="txtTitle" name="txtTitle"></td> 
 
      <td>&nbsp;</td> 
 
     </tr> 
 
     <tr> 
 
      <td>File</td> 
 
      <td> <input type="file" name="file[]" id="txtTitle" name="txtTitle"></td> 
 
      <td>&nbsp;</td> 
 
     </tr> 
 
    </table> 
 
    <table> 
 
     <tr> 
 
      <td style="width: 33px;"></td> 
 
      <td style="width: 183px;">&nbsp;</td> 
 
      <td><input type="button" id="addbutton" value="Add Row"></td> 
 
     </tr> 
 
    </table>

+0

啊,讓我困惑,解決方案只是刪除tr:首先,非常感謝你:) @Gary – Zeus234