Javascript 如果复选框被标记,则复制TR元素并附加到外部源
我有这个jQuery代码:Javascript 如果复选框被标记,则复制TR元素并附加到外部源,javascript,jquery,html,Javascript,Jquery,Html,我有这个jQuery代码: $('button#btnBuscar').on('click', function (ev) { ev.preventDefault(); $.post(Routing.generate('filtrarNormas'), $('#buscadorNorma').serialize(), 'json') .done(function (data, textStatus, jqXHR) { if
$('button#btnBuscar').on('click', function (ev) {
ev.preventDefault();
$.post(Routing.generate('filtrarNormas'), $('#buscadorNorma').serialize(), 'json')
.done(function (data, textStatus, jqXHR) {
if (data.entities.length > 0) {
$('#resultadoNorma').show();
var $html = '';
data.entities.forEach(function (value, index, array) {
$html += '<tr>';
$html += '<td><input type="checkbox" value="' + value.id + '"></td>';
$html += '<td>' + value.codigo + '</td>';
$html += '<td>' + value.norma + '</td>';
$html += '<td>' + value.anno + '</td>';
$html += '<td>' + value.comiteTecnico + '</td>';
$html += '</tr>';
});
$("table tbody#resultadoNormaBody").html($html);
}
})
.fail();
});
但是我该怎么做呢
注意:tr
应该克隆的表不在模式中,而是在同一页中,因此这不应该是一个问题
$('#resultadoNormaBody').on('change','input[type=checkbox]',function(){
var my_checkbox = $(this);
if (my_checkbox.is(':checked')) {
my_checkbox.closest('tr').clone().appendTo('#newtable');
}
});
更新(OP对预期行为做出一些澄清后):
要在默认情况下禁用按钮,请在HTML标记中包含disabled属性:
<button id="btnAplicarNorma" disabled>Copy Rows</button>
您的解决方案会起作用,是的,但是如果我多次切换复选框会发生什么?同一行将附加到
#newTable
,这不是我的想法,如果我按照您的建议离开,那么我应该将附加部分切换到,我不知道,我的想法是有一个按钮(抱歉,在主帖子上没有提及),并标记所有我想要附加的复选框,然后单击按钮的事件将它们附加到新表,类似于$('button#btnaplicarnma')。在('click',function(ev){//检查标记了哪些复选框并克隆当前元素})代码>怎么做?那是另一个问题,继续编辑你的原创,我会编辑我的答案!完成后,看一看并告诉我这是否足够,谢谢你的回答它工作得很好只有一件事,clone()
准确地复制tr
元素,包括标记的复选框,在传递到第二个表之前,是否可以取消标记复选框?好的,在您的示例中,您应该附加到tbody元素appendTo(“#normaBody”)。您还需要添加一些功能来关闭灯箱并显示隐藏的表。否则它看起来像是在工作。
<button id="btnAplicarNorma" disabled>Copy Rows</button>
$('#resultadoNormaBody').on('change','input[type=checkbox]',function(){
var $my_checkbox = $(this);
var $my_tr = $my_checkbox.closest('tr');
if ( $my_checkbox.prop('checked') ) {
// Add the class to mark that it should be copied later when button clicked
$my_tr.addClass('copyMe');
}
var $all_checkboxes = $my_checkbox.closest('tbody').find('input[type=checkbox]');
// Loop through checkboxes to see if one is checked
$all_checkboxes.each(function() {
// if one checkbox is checked, enable button and end the .each loop
if ( $(this).prop('checked') ) {
$('#btnAplicarNorma').prop('disabled',false);
return false;
}
// If we didn't catch any checked boxes earlier, disable the button
$('#btnAplicarNorma').prop('disabled',true);
});
});
$('#btnAplicarNorma').on('click', function (ev) {
var $tr_to_append = $('#resultadoNormaBody').find('tr.copyMe');
// is there's a tr to copy, clone it, append, and remove the copyMe classes
if ( $tr_to_append.length ) {
// first uncheck all the checkboxes
$tr_to_append.find('input[type=checkbox]').prop('checked',false);
// copy and append and remove class
$tr_to_append.clone().appendTo('#newtable').removeClass('copyMe');
$tr_to_append.removeClass('copyMe');
// disable the button again
$(this).prop('disabled',true);
}
});