[英]How to get specific HTML control from a <table><tr><td></td></tr></table> and append the selected control to the same <td>?
I've following table in HTML: 我已经按照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=""> Add</button></td>
<td colspan="5"></td>
</tr>
</tfoot>
</table>
Now I've to append the 现在我要附加
<select name="product_id_1[1]" id="product_id_1_1" class="form-control prod_list"> <option value="" selected='selected'>Select Product</option> </select>
to the <td>
in which it is present currently on the click of Add button. 单击“添加”按钮上当前显示的
<td>
。 Previously the functionality was of adding the entire row with <select></select>
in first <td>
of a new <tr>
but now the requirement is to append the <select></select>
to the existing <td>
only. 以前的功能是在新
<tr>
第一个<td>
中使用<select></select>
添加整个行,但现在的要求是仅将<select></select>
追加到现有的<td>
。 My previous code is as follows: 我以前的代码如下:
$(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">×</button>').appendTo( $(new_row.find('td:first')) );
tbody.append(new_row);
$(new_row).children('td').not('td:eq(0)').remove();
});
But please note that the id and name of the <select></select>
to be appended should be in same fashion as of my previous functionality. 但是请注意,要附加的
<select></select>
的ID和名称应与我以前的功能相同。 You can refer above code for it. 您可以参考上面的代码。 Thanks in advance.
提前致谢。
you can try this below javascript code, 您可以在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">×</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();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.