1
<tr>
    <td>
        <input type = "checkbox" name = "chk"/>
    </td>
    <td>
        <select name = "foo">
            <option value = "1">One</option>
            <option value = "2">Two</option>
        </select>
    </td>
    <td>
        <select name = "bar">
            <option value = "1">One</option>
            <option value = "2">Two</option>
        </select>
    </td>
</tr>

<tr>
    <td>
        <input type = "checkbox" name = "chk"/>
    </td>
    <td>
        <select name = "foo">
            <option value = "1">One</option>
            <option value = "2">Two</option>
        </select>
    </td>
    <td>
        <select name = "bar">
            <option value = "1">One</option>
            <option value = "2">Two</option>
        </select>
    </td>
</tr>

我想克隆带有选中复选框的选定项目的行。

我正在使用它,但它只是复制文本字段而不是所选项目。

lastRow = $('#dataTable tr').has('input:checked').html();
$('#dataTable tr:last').after('<tr>'+lastRow+'</tr>');
$('#dataTable tr:last').find('select').each(function()
{
    var this_select=$(this);
    this_select.val(this_select.closest('tr').prev().find('td:eq('+this_select.closest('td').index()+')').find('select').val());
});
4

1 回答 1

3

尝试类似的东西

var srcrow = $('#dataTable tr').has('input:checked');
var lastRow = srcrow.clone();

lastRow.find('select').each(function(idx, el){
    var $el = $(el);
    $el.val(srcrow.find('select').eq(idx).val())
});

$('#dataTable tr:last').after(lastRow);

演示:小提琴

支持多个检查项的更复杂的实现

演示:小提琴

于 2013-07-04T06:01:58.833 回答