开发者

trigger click problem on isset (Php, jquery, iframe)

I am having a small problem and i don`t know the reason. I set that

1- If you click Print button then print.php page will be printed automatically.

2- if you submit a form then print.php page will be printed automatically.

Problem is on second option. Page gets printed automatically perfect but if i click Print button again it does not print the page again.

I am using firefox.

<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

    <script type="text/javascript"> 
    function loadiFrame(src) 
    { 
        $("#iframeplaceholder").html("<iframe id='myiframe' name='myname' src='" + src + "' frameborder='0'  vspace='0'  hspace='0'  marginwidth='0'  marginheight='0' width='1'  scrolling='no'  height='1' />"); 
    } 

    $(function() 
    { 
        $("#printbutton")开发者_如何学编程.bind("click",  
            function() {  
                loadiFrame('print.php');  
                $("#myiframe").load(  
                    function() { 
                        window.frames['myname'].focus(); 
                        window.frames['myname'].print();     
                     } 
                 ); 
            } 
        ); 
    }); 
    </script>

<?
if (isset($_POST['formSubmit'])) 
{
echo "form submitted";
?>   
<script type="text/javascript"> 
    $(document).ready(function(){ 
    $('#printbutton').trigger('click'); 
   });
</script>  
<? } ?>  
</head>

<body>
<table border="1">
  <tr>
    <td><input type="button" id="printbutton" value="  Print  " /><div id="iframeplaceholder"></div></td>
  </tr>
</table>


<form action="auto.php" method="post">
<input name="formSubmit" type="submit" value="Submit" />
</form>
</body>


you should call loadiFrame function only if the iframe doesn't exist.

$(function() 
{ 
    $("#printbutton").click(  
        function() {
    if($('#myiframe').length==0){ 
            loadiFrame('print.php'); }
            $("#myiframe").ready(  
                function() { 
                    window.frames['myname'].focus(); 
                    window.frames['myname'].print();     
                 }); 
        }); 
}); 

Try it now.

Good luck :)


it's a bug in Firefox. It works if you add a counter that makes each iframe name unique.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜