如何从<table> <tbody> <tr> <td> </td> </tr> </tbody> </table>获取特定的HTML控件并将所选控件附加到相同的控件上?

时间:2014-05-12 05:47:10

标签: javascript jquery html-table html-select jquery-append

我在HTML中跟随表:

<table id="blacklistgrid_1"  class="table table-bordered table-hover table-striped">
  <thead>
    <tr>
      <th style="vertical-align:middle">Products</th>
      <th style="vertical-align:middle">Pack Of</th>
      <th style="vertical-align:middle">Quantity</th>
      <th style="vertical-align:middle">Volume</th>
      <th style="vertical-align:middle">Unit</th>
      <th style="vertical-align:middle">Rebate Amount</th>
    </tr>
  </thead>
  <tbody class="apnd-test">
    <tr id="reb1_1">
      <td>
        <div class="btn-group">
          <select name="product_id_1[1]" id="product_id_1_1" class="form-control prod_list">
            <option value=""  selected='selected'>Select Product</option>
          </select>
        </div>
      </td>
      <td><input type="text" name="pack[1]" id="pack_1" value="" class="form-control" size="8"/></td>
      <td><input type="text" name="quantity[1]" id="quantity_1" value="" class="form-control" size="8"/></td>
      <td><input type="text" name="volume[1]" id="volume_1" value="" class="form-control" size="8"/></td>
      <td>
        <div class="btn-group">
          <select name="units[1]" id="units_1" class="form-control">
            <option value=""  selected='selected'>Select Unit</option>
            <option value="5" >Microsecond</option>
            <option value="7" >oz</option>
            <option value="9" >ml</option>
            <option value="10" >L</option>
            <option value="12" >gms</option>
          </select>
        </div>
      </td>
      <td><input type="text" name="amount[1]" id="amount_1" value="" class="form-control" size="9"/></td>
    </tr>
  </tbody>
  <tfoot>
    <tr id="reb1_2">
      <td><button style="float:right; margin-bottom: 20px" class="products" type="button" class="btn btn-default" onclick="">&nbsp;Add</button></td>
      <td colspan="5"></td>                            
    </tr>
  </tfoot>                                           
</table>

现在我要追加

<select name="product_id_1[1]" id="product_id_1_1" class="form-control prod_list"> <option value="" selected='selected'>Select Product</option> </select>

到当前点击“添加”按钮时出现的<td>。以前,该功能是在新<select></select>的第一个<td>中添加<tr>整行,但现在要求将<select></select>附加到现有<td> } 只要。我以前的代码如下:

$(document).delegate('.products','click',function (e) {
    var table_id = $(this).closest('table').attr('id');
    var no = table_id.match(/\d+/)[0];            
    var first_row = $('#'+table_id).find('tbody tr:first').attr('id');
    var new_row = $('#'+first_row).clone();
    var tbody = $('#' + table_id + ' tbody');
    var n = $('tr', tbody).length  + 1;
    new_row.attr('id', 'reb' + no +'_'+ n);    

    $(':input', new_row).not('.prod_list').remove();
    $('select', new_row).attr('name','product_id_'+no+'['+n+']');
    $('select', new_row).attr('id','product_id_'+no+'_'+n);
    $('<button style="color:#C00; opacity: 2;margin-top: 6px;" type="button" class="close delete" data-dismiss="alert" aria-hidden="true">&times;</button>').appendTo( $(new_row.find('td:first')) );
    tbody.append(new_row);

    $(new_row).children('td').not('td:eq(0)').remove();    
  });

但请注意,要附加的<select></select>的ID和名称应与我以前的功能相同。你可以参考上面的代码。提前谢谢。

1 个答案:

答案 0 :(得分:1)

你可以在javascript代码下面试试这个,

var count = 1;
$('#blacklistgrid_1').on('click', '.products', function () {
    var n = $('.pro:last').attr('name').match(/\[(.*?)\]/);
    var newName = $('.pro:last').attr('name').replace(/\[(.*?)\]/, '[' + (+n[1] + 1) + ']');
    var newID = $('.pro:last').attr('id').replace(/[^_]*$/, +n[1] + 1);
    var toCloneDiv = $('.selectControls:last').clone();
    var clonedDiv = $(toCloneDiv).appendTo('#reb1_1 > td:nth-child(1)');
    //add delete button
    if (count == 1) $('<button style="color:#C00; opacity: 2;margin-top: 6px;" type="button" class="close delete" data-dismiss="alert" aria-hidden="true">&times;</button>').appendTo(clonedDiv);
    count++;
    $(clonedDiv).find('.pro').attr({
        'id': newID,
            'name': newName
    });
});
// to remove select element
$('#blacklistgrid_1').on('click','.close', function(){
    $(this).closest('div').remove();
});

SEE THIS FIDDLE DEMO