0

我有代码在单击按钮打印窗口打开后打印选定的 div 区域但问题是当我在文本框中输入内容然后单击按钮时文本框值从页面中消失并且文本框值也没有出现在打印中窗户?

<script>
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
</script>

<div id="printableArea">
<input type="text" >
</div>
<input type="button" onclick="printDiv('printableArea')" value="print a div!" /> 
4

2 回答 2

0

I have altered your code to retain the textbox value:

<html>
<head>
<script>
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
var text=document.getElementById("t1").value;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
document.getElementById("t1").value=text;
}
</script>
</head>
<body>
<div id="printableArea">
<input type="text" id="t1">
</div>
<input type="button" onclick="printDiv('printableArea')" value="print a div!" /> 
</body>
</html>
于 2013-03-25T12:27:09.587 回答
0
function printDiv(divName) {
    var inpText = document.getElementsByTagName("input")[0].value;
    var printContents = document.getElementById(divName).innerHTML;
    var originalContents = document.body.innerHTML;
    document.body.innerHTML = printContents;
    document.getElementsByTagName("input")[0].value = inpText;
    window.print();
    document.body.innerHTML = originalContents;
    document.getElementsByTagName("input")[0].value = inpText;
}
于 2013-03-25T12:29:04.090 回答