复制选中复选框选择的行的选定值

时间:2013-07-04 05:53:53

标签: javascript jquery html

<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());
});

1 个答案:

答案 0 :(得分: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);

演示:Fiddle

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

演示:Fiddle