我无法打印所有选定的复选框!(阵列)



我正在努力让我的二维码排队打印,我有一个二维码列表,想检查并打印 SELECTED,我快到了。 但我卡在迭代上。当我单击打印时,默认情况下它只打印数组[0]。我想在弹出的打印窗口中打印所有已切换的项目。 这是我的代码: 大家好 我正在努力让我的二维码排队打印,我有一个二维码列表,想检查并打印选定的,我快到了。但我被困在迭代上。当我单击打印时,默认情况下它只打印数组[0]。我想在弹出的打印窗口中打印所有已切换的项目。 这是我的代码:

<script type="text/javascript">
function PrintQR() {
var codes = document.getElementsByClassName("QR")[0].innerHTML
for (var i = 0; i < codes.length; i++) {
var popupWin = window.open('', 'blank');
popupWin.document.open();
popupWin.document.write('' + '<html>' + '<body onload="window.print()">' + '<div class="Q">' + codes + '</div>' + '</body>' + '</html>');
popupWin.document.close();
setTimeout(function() {
popupWin.close();
}, 10);
}
};
</script>
//MY Submit Button Here :
<div>
<center><button type="button" class="btn btn-primary btn-sm" onclick="PrintQR()"> Print This </button></center>
</div>
Here is my FORM:
<form method="POST">
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['qrcode'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['qrcode']);
echo "You have selected following " . $checked_count . " option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach ($_POST['qrcode'] as $selected) {
$output = '
<br><br><br>
<div class="QR">
<center><img src="userQr/' . $selected . '" /> </center>
</div>
';
echo $output;
}
}
}
?>
</form>

它只是打印第一个数组,有时当我尝试时,它会给我一个"HTML 集合"Javascript 错误! 任何帮助将不胜感激。 感谢大家在Stackoverflow的聪明工作。 艾德-

您在此处仅显式选择第一个元素:

var codes = document.getElementsByClassName("QR")[0].innerHTML

您应该选择所有元素及其上的循环。下面是一个示例:

function PrintQR() {
var codes = document.querySelectorAll('.QR');
codes = [...codes].map(code => code.innerHTML);
console.log(codes.join('n'));
// Uncomment the following on your real page
/*
var popupWin = window.open('', 'blank');
popupWin.document.open();
popupWin.document.write('' + '<html>' + '<body onload="window.print()">' + '<div class="Q">' + code + '</div>' + '</body>' + '</html>');
popupWin.document.close();
setTimeout(function() {
popupWin.close();
}, 10);
*/
};
<div>
<center><button type="button" class="btn btn-primary btn-sm" onclick="PrintQR()"> Print This </button></center>
</div>
<form method="POST">
You have selected following 3 option(s): <br />
<br><br><br>
<div class="QR">
<center><img src="userQr/1" /> </center>
</div>
<br><br><br>
<div class="QR">
<center><img src="userQr/2" /> </center>
</div>
<br><br><br>
<div class="QR">
<center><img src="userQr/3" /> </center>
</div>
</form>

更新
由于您希望在一次运行中打印所有图像,因此无需循环访问它们。上面的代码片段已更新,以显示一个没有循环的示例。 您可能需要一些CSS样式才能将图像打印在不同的页面上。

谢谢先生,但现在它正在打印 ONLy 最后一个,通过打印我的意思是打印到打印机,将显示一个弹出窗口,唯一显示的二维码是数组中的最后一个...... 我真的希望它们在打印前在弹出的"打印预览"窗口中显示所有内容,是的,我选择了"全部页面"。 非常感谢。! 艾德-

最新更新