admin管理员组

文章数量:1392086

<html>
<head>
<script type="text/javascript">
    function printpage()
        {document. getElementById ('print').style.display='none';
        window.print()
    }
</script>
</head>
<body>
  <td align="center">
    <input  name="print" type="submit" id="print" value="PRINT" onclick="printpage()" />
  </td>
</body>
</html>

When I click, it will open a print window. Once I close print window and open it again and then click on print, without reloading, then it will not work.

<html>
<head>
<script type="text/javascript">
    function printpage()
        {document. getElementById ('print').style.display='none';
        window.print()
    }
</script>
</head>
<body>
  <td align="center">
    <input  name="print" type="submit" id="print" value="PRINT" onclick="printpage()" />
  </td>
</body>
</html>

When I click, it will open a print window. Once I close print window and open it again and then click on print, without reloading, then it will not work.

Share Improve this question edited Jul 31, 2012 at 10:54 Tim M. 54.4k14 gold badges124 silver badges166 bronze badges asked Jul 31, 2012 at 10:47 M2KM2K 331 silver badge5 bronze badges 5
  • 1 What is it that you're trying to attain? – asprin Commented Jul 31, 2012 at 10:49
  • 7 Please take the time to format your code and properly space it. We should not have to labour to read your code. If you can't be bothered to even put line breaks in, why should anyone help? – Mitya Commented Jul 31, 2012 at 10:49
  • 7 Wondering how you clicked on the print button again if it was hidden? – Ashwin Prabhu Commented Jul 31, 2012 at 10:52
  • 1 First, there is no PHP involved in this isse (wrong tag). Second, as Ashwin said, how did you click the print button a second time since it is being hidden after the frist click? Did you mean that you can't find the button after you clicked it? That's because it's hidden. Solution: add document.getElementById('print').style.display='block'; after window.print(); – MiDo Commented Jul 31, 2012 at 10:54
  • 1 @user1500506: see my answer having solution with css media queries. – Ahsan Khurshid Commented Jul 31, 2012 at 11:00
Add a ment  | 

3 Answers 3

Reset to default 7

Remove document.getElementById('print').style.display='none'; from your printpage() function.

In the above case the button will be visible for another click event but when you will print the document, the button will be shown on printed document. Am I right?

To prevent printing the print button you need to use css media queries @media print

Add following in your extrernal stylesheet OR in <style> tag inside a <head> tag of the HTML page:

 @media print {    
     .noprint { display: none; }
 }​

and add .noprint class on

<input  name="print" class="noprint" type="submit" id="print" value="PRINT" onclick="printpage()" />

SEE DEMO

It will print the document without printing the button and your button will also be visible for the second time click :-)

EDITED:

USE HTML AS GIVEN BELOW:

<!DOCTYPE html>
<html>
    <head>

      <meta charset=utf-8 />  
      <title>JS Bin</title>  

      <!-- Your Stylesheet (CSS) -->  
      <style type="text/css">
         @media print {    
           .noprint { display: none; }
         }​
      </style>

      <!-- Your Javascript Function -->  
      <script>
          function printpage() {       
              window.print();
          }
      </script>

    </head>
<body>

<!-- Your Body -->
<p>Only This text will print</p>

<!-- Your Button -->
<input class="noprint" type="button" value="PRINT" onclick="printpage()" />

</body>
</html>

SEE ABOVE CODE IN ACTION

Change type=submit to type=button

You are unloading the page when submitting

And hide the button with CSS as posted by A.K.

Just remove

document.getElementById('print').style.display='none';

from your printpage() function. It'll work fine

If you don't want the print button on your printed page,

document.getElementById('print').style.visibility='hidden'
window.print();
document.getElementById('print').style.visibility='visible'

本文标签: javascriptWhy does it print only one time Please give me solutionStack Overflow