简体   繁体   English

如何在打印预览中获取输入文本字段值?

[英]How to get input text field values in print preview?

Here is a sample code what I am trying to fix. 这是我要修复的示例代码。 In print screen I am not able to see the text which is entered in input text field. 在打印屏幕中,我看不到在input文本字段中input文本。 Any solution will be appreciated. 任何解决方案将不胜感激。

 var divToPrint=document.getElementById('div1Id');

 if (divToPrint != null && divToPrint != "") {
   var newWin=window.open('','Print-Window');
   newWin.document.open();
   newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
   newWin.document.close();
   newWin.close();    
 }

Textbox and Checkbox controls you have to get value instead of InnerHtml 文本框和复选框控件必须获得value而不是InnerHtml

   var divName = document.getElementById('div1Id')
    var originalContents = document.body.innerHTML;
    var inpText = document.getElementsByTagName("input")[0].value;
    var printContents = document.getElementById(divName).innerHTML;
    printContents += inpText;

    newWin.document.open();
    newWin.document.write('<html><body 
    onload="window.print()">'+printContents+'</body></html>');
    newWin.document.close();       

You have this script 你有这个script

 var divToPrint=document.getElementById('div1Id');

 if (divToPrint != null && divToPrint != "") {
   var newWin=window.open('','Print-Window');
   newWin.document.open();
   newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
   newWin.document.close();
   newWin.close();    
 }

where you copy the inner html, however, the value is not there. 您在其中复制内部html的位置,但该值不存在。 Let's make sure the values are correct, like this: 让我们确保值是正确的,如下所示:

 var divToPrint=document.getElementById('div1Id');

 //Gather input values and inputs
 var inputValues = [];
 var inputs = divToPrint.getElementsByTagName("input");
 for (var index = 0; index < inputs.length; index++) inputValues.push(inputs[index]);

//Generate script of page
var script = 'var inputValues = ' + JSON.stringify(inputValues) + ';';

script += 'var inputs = document.getElementsByTagName("input");' +
          'for (var index = 0; index < inputs.length; index++) ' +
          'inputs[index].value = inputValues[index];';

 if (divToPrint != null && divToPrint != "") {
   var newWin=window.open('','Print-Window');
   newWin.document.open();
   newWin.document.write('<html><body onload="' + script + 'window.print()">'+divToPrint.innerHTML+'</body></html>');
   newWin.document.close();
   newWin.close();    
 }

Code is untested, please let me know if there is a typo. 代码未经测试,请告诉我是否有错字。

使用jquery.print.js库并在您的自定义函数$ .print(“#id”)中调用此函数;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM