使用PHP在IE上使用javascript警告两次问题

时间:2013-11-25 07:44:09

标签: javascript php html

关于IE的问题,如果我在IE以外的所有浏览器中运行此脚本,它会提交一次提醒表单,但如果我尝试IE,则会提醒两次,

可能是什么原因?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
if(isset($_POST['btn_submit']) && $_POST['btn_submit'] == 'Submit')
{
    ?>
        <script language="javascript">  
        alert('form submitted');
        </script>    
    <?php   
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="name" method="post">
    <input type="file"/>
    <input type="submit" name="btn_submit" value="Submit" />
</form> 
</body>
</html>

编辑:我在IE7和IE8上试过这段代码

2 个答案:

答案 0 :(得分:2)

在脚本末尾尝试你的js:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="name" method="post">
    <input type="file"/>
    <input type="submit" name="btn_submit" value="Submit" />
</form> 
</body>
</html>
<?php
if(isset($_POST['btn_submit']) && $_POST['btn_submit'] == 'Submit')
{
    ?>
        <script language="javascript">  
        alert('form submitted');
        </script>    
    <?php   
}
?>

答案 1 :(得分:0)

尝试这种方式,它有点干净的方式。

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Untitled Document</title>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script type="text/javascript"> 
  $(document).ready(function() {
    $('#name').submit(function(event){
         event.preventDefault();
         // Do other stuff   
         alert('form submitted');
    });
   });
  </script>
  </head>
  <body>
     <form id="name" method="post">
        <input type="file"/>
        <input type="submit" name="btn_submit" value="Submit" />
     </form> 
  </body>
  </html>