你有這樣的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();
}
代碼未經測試,請讓我知道是否有錯字。
'divToPrint.innerHTML'應該是'divToPrint.value',因爲您想從該輸入中獲得實際值 – Jer
輸入文本字段沒有'innerHTML'屬性,您應該使用'.value'獲取輸入文本值 –
注意:'divToPrint'永遠不會等於一個字符串,('!=「」'),因爲它是對'getElementById'的調用的結果,它返回一個元素或null。你的條件可以簡單地爲'if(divToPrint){'。 – Utkanos