javascript – AJAX:向表中添加新行或使用AJAX删除

前端之家收集整理的这篇文章主要介绍了javascript – AJAX:向表中添加新行或使用AJAX删除前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是逻辑:
我输入了一些表格,表格是 AJAX实时搜索.在找到值之后,我点击“添加”按钮,它会在现有的表/ tbody中创建新行.
  1. <table class="standard">
  2. <thead>
  3. <tr>
  4. <td colspan="2">
  5. Start Input barcode / Product Name
  6. </td>
  7. <td colspan="4">
  8. <input type="text" size="90" value="" placeholder="Barcode / Product Name">
  9. </td>
  10. <td>
  11. <button class="tambah"><i class="icon-plus"></i> Add</button>
  12. </td>
  13. </tr>
  14.  
  15. <tr>
  16. <td>
  17. No.
  18. </td>
  19. <td>
  20. Kode Barang
  21. </td>
  22. <td>
  23. Nama Barang
  24. </td>
  25. <td>
  26. Qty
  27. </td>
  28. <td>
  29. Harga
  30. </td>
  31. <td>
  32. Disc %
  33. </td>
  34. <td>
  35. Total
  36. </td>
  37. </tr>
  38. </thead>
  39. <tbody>
  40.  
  41. <!-- when button add is click that will add <tr></tr> here -->
  42. </tbody>
  43. </table>

我能这样做吗?如果是这样,怎么样?

小提琴示例:http://jsfiddle.net/anggagewor/cauPH/

解决方法

试试这个
  1. var scntDiv = $('#p_scents');
  2. var i = $('#p_scents tr').size() + 1;
  3.  
  4. $('#addScnt').click(function() {
  5. scntDiv.append('<tr><td><select name="type" id="type"><option value="Debit">Debit</option><option value="Credit">Credit</option></select></td><td><select name="accounts" id="accounts"><option value="">SELECT</option><option value="One">One</option><option value="Two">Two</option></select></td><td><input type="text" name="debit_amount" id="debit_amount"/></td><td><input type="text" name="credit_amount" id="credit_amount"/></td><td><a href="#" id="remScnt">Remove</a></td></tr>');
  6. i++;
  7. return false;
  8. });
  9.  
  10. //Remove button
  11. $(document).on('click','#remScnt',function() {
  12. if (i > 2) {
  13. $(this).closest('tr').remove();
  14. i--;
  15. }
  16. return false;
  17. });​

这是一个工作示例,包括删除功能DEMO.

猜你在找的Ajax相关文章