英文:
how to copy multiple checked rows in html via JS
问题
我有一个包含表格的HTML Blade页面,我正在尝试使用以下脚本复制HTML中选中的行:
<script>
    document.addEventListener('DOMContentLoaded', function() {
        $('#checkAll').on('change', function() {
            $('.rowCheckbox').prop('checked', this.checked);
        });
        const copySelectedButton = document.getElementById('copySelected');
        copySelectedButton.addEventListener('click', function() {
            const selectedRows = document.querySelectorAll('input.rowCheckbox:checked');
            let textToCopy = '';
            selectedRows.forEach(row => {
                const text = row.getAttribute('data-clipboard-text');
                textToCopy += text + '\n';
            });
            if (selectedRows.length > 0) {
                copyToClipboard(textToCopy);
                iziToast.success({
                    title: 'Done',
                    message: 'Done',
                    position: 'bottomRight'
                });
            } else {
                iziToast.info({
                    title: 'No Records',
                    message: 'No Records',
                    position: 'bottomRight'
                });
            }
        });
        function copyToClipboard(text) {
            const tempTextarea = document.createElement('textarea');
            tempTextarea.value = text;
            document.body.appendChild(tempTextarea);
            tempTextarea.select();
            document.execCommand('copy');
            document.body.removeChild(tempTextarea);
        }
    });
</script>
它能够工作,但是每一行都返回空值,换句话说,我得到了复制的结果,但是没有得到每一行的值,而是得到了以下结果:
<br>
null
<br>
null
<br>
null
<br>
null
<br>
null
如果我选中了5行。
我正在使用Laravel,并且该代码位于视图中:
<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th style="width: 5%;"><input type="checkbox" id="checkAll"></th>
            <th style="width: 50%;">agent</th>
            <th style="width: 20%;">curr</th>
            <th style="width: 20%;">ras</th>
            <th style="width: 5%;">copy</th>
        </tr>
    </thead>
    <tbody>
        @foreach($result as $x)
        <tr>
            <td><input type="checkbox" class="rowCheckbox"></td>
            <td>{{ $x->AccName }}</td>
            <td>{{ $x->AccCurrName }}</td>
            <td>{{ number_format($x->AccRaseed, 0) }}</td>
            <td>
                <a class="btn btn-sm btn-info btncopy" data-clipboard-text="agent: {{ $x->AccName }}
curr: {{ $x->AccCurrName }}
ras: {{ number_format($x->AccRaseed, 0) }}">
                    <i class="las la-copy"></i>
                </a>
            </td>
        </tr>
        @endforeach
    </tbody>
</table>
请问问题出在哪里?
英文:
I have a HTML blade page that contains a table and I'm trying to copy the checked rows in HTML by using the following script:
<script>
    document.addEventListener('DOMContentLoaded', function() {
        $('#checkAll').on('change', function() {
            $('.rowCheckbox').prop('checked', this.checked);
        });
        const copySelectedButton = document.getElementById('copySelected');
        copySelectedButton.addEventListener('click', function() {
            const selectedRows = document.querySelectorAll('input.rowCheckbox:checked');
            let textToCopy = '';
            selectedRows.forEach(row => {
                const text = row.getAttribute('data-clipboard-text');
                textToCopy += text + '\n';
            });
            if (selectedRows.length > 0) {
                copyToClipboard(textToCopy);
                iziToast.success({
                    title: 'Done',
                    message: 'Done',
                    position: 'bottomRight'
                });
            } else {
                iziToast.info({
                    title: 'No Records',
                    message: 'No Records',
                    position: 'bottomRight'
                });
            }
        });
        function copyToClipboard(text) {
            const tempTextarea = document.createElement('textarea');
            tempTextarea.value = text;
            document.body.appendChild(tempTextarea);
            tempTextarea.select();
            document.execCommand('copy');
            document.body.removeChild(tempTextarea);
        }
    });
</script>
it's working but returns Null values in each row, in the other saying I got the copied results but I don't got the values for each row I got instead
<br>
null
<br>
null
<br>
null
<br>
null
<br>
null
if I checked 5 rows
I'm using Laravel and that code inside view.
<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th style="width: 5%;"><input type="checkbox" id="checkAll"></th>
            <th style="width: 50%;">agent</th>
            <th style="width: 20%;">curr</th>
            <th style="width: 20%;">ras</th>
            <th style="width: 5%;">copy</th>
        </tr>
    </thead>
    <tbody>
        @foreach($result as $x)
        <tr>
            <td><input type="checkbox" class="rowCheckbox"></td>
            <td>{{ $x->AccName }}</td>
            <td>{{ $x->AccCurrName }}</td>
            <td>{{ number_format($x->AccRaseed, 0) }}</td>
            <td>
                <a class="btn btn-sm btn-info btncopy" data-clipboard-text="agent: {{ $x->AccName }}&#13;curr: {{ $x->AccCurrName }}&#13;ras: {{ number_format($x->AccRaseed, 0) }}">
                    <i class="las la-copy"></i>
                </a>
            </td>
        </tr>
        @endforeach
    </tbody>
</table>
what's the problem please
答案1
得分: 0
我添加了一个变量来保存数据:
@php
    $lineBreak = "\n";
    $clipboardText = "姓名:" . $x->AccName . $lineBreak . "货币:" . $x->AccCurrName . $lineBreak . "余额:" . number_format($x->AccRaseed, 0). $lineBreak;
    @endphp
然后我将复选框更改为:
 <td><input type="checkbox" class="rowCheckbox" data-clipboard-text="{{ $clipboardText }}"></td>
英文:
I added variable to keep the data:
@php
    $lineBreak = "\n";
    $clipboardText = "الاسم: " . $x->AccName . $lineBreak . "العملة: " . $x->AccCurrName . $lineBreak . "الرصيد: " . number_format($x->AccRaseed, 0). $lineBreak;
    @endphp
and then I changed the checkbox to be:
 <td><input type="checkbox" class="rowCheckbox" data-clipboard-text="{{ $clipboardText }}"></td>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论