[英]How to get input values from print preview using jquery/javascript
我有带有字段的示例html内容,我需要打印所有内容,包括输入值,并将其显示在示例预览中。 我尝试使用以下脚本触发打印:
样品打印工作正常,但是未显示输入值。 我该如何实现? 我应该使用html()函数之外的替代方法吗?
function PrintPreview() { printWindow = window.open("", "", "location=1,status=1,scrollbars=1,width=650,height=600"); printWindow.document.write($('#mainbody').html()); printWindow.document.close(); printWindow.focus(); }
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> <div id="mainbody"> Last Name <input type="text" id="lname" name="lname">First Name <input type="text" id="fname" name="fname">Middle Name <input type="text" id="mname" name="mname"> </div> <input type="button" onclick="PrintPreview();" value="Print Preview" />
这对我有用-将值复制到value属性并克隆div
function PrintPreview() {
$("#mainbody > input").each(function() {
$(this).attr("value",$(this).val());
});
var printWindow = window.open("", "_blank", "location,status,scrollbars,width=650,height=600");
if (printWindow) {
printWindow.document.write($('#mainbody').clone(true)[0].innerHTML);
printWindow.document.close();
printWindow.focus();
}
else alert("Please allow popups");
}
或者
您可以将以下代码放在PrintPreview函数之前:
$('input').each(function(){
$(this).keyup(function(){
$(this).attr('value',$(this).val());
});
});
这样,就可以在keyup上更改value属性,并且预览可以正常工作。
这一次尝试。
<div id="mainbody">
Last Name <input type="text" id="lname" name="lname">
First Name<input type="text" id="fname" name="fname">
Middle Name<input type="text" id="mname" name="mname">
</div>
<input type="button" onclick="PrintPreview();" value="Print Preview" />
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
function PrintPreview() {
window.print();
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.