簡體   English   中英

如何在打印預覽中獲取輸入文本字段值?

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

這是我要修復的示例代碼。 在打印屏幕中,我看不到在input文本字段中input文本。 任何解決方案將不勝感激。

 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();    
 }

文本框和復選框控件必須獲得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();       

你有這個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();    
 }

您在其中復制內部html的位置,但該值不存在。 讓我們確保值是正確的,如下所示:

 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();    
 }

代碼未經測試,請告訴我是否有錯字。

使用jquery.print.js庫並在您的自定義函數$ .print(“#id”)中調用此函數;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM