qq_18136213 2017-02-25 16:02 采纳率: 33.3%
浏览 3760
已采纳

php提交表单不刷新页面的方法(当前页面)

<!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=gb2312" />
<title>计算器</title>
<script type="text/javascript">
//document.getElementById("sub1").submit();
function setval(){
var gd = document.getElementById("gaodu").value;
var kd = document.getElementById("kuandu").value;
var sl = document.getElementById("shuliang").value;
if(gd=="")
document.getElementById("gaodu").value=1600;
if(kd=="")
document.getElementById("kuandu").value=600;
if(sl=="")
document.getElementById("shuliang").value=1;
}
</script>
</head>
<?php  
     $gaodu=$_GET["gaodu"];
     $kuandu=$_GET["kuandu"];
     $shuliang=$_GET["shuliang"];
?>     
<body onload="setval()">  
<form action="" id="sub1">  
<input name="gaodu" type="text" id="gaodu" onchange="tijiao()" value="<?php echo $gaodu ?>"> 
<input name="kuandu" type="text" id="kuandu" onchange="tijiao()" value="<?php echo $kuandu ?>">
<input name="shuliang" type="text" id="shuliang" onchange="tijiao()" value="<?php echo $shuliang ?>"> 
<input name="sub" id="sub" type="submit"  value="计算">
</form>
<table width="200" border="1">
  <tr>
    <td><?php echo $gaodu+$kuandu ?></td>
    <td><?php echo $gaodu-$kuandu ?></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>  
</html>

以上代码是想做一个特定功能的计算器,为了初始化页面数据,用js设置了表单默认值,但是页面加载时无法自动计算结果(试过js代码onload提交表单,但是页面会不停的刷新)请教提交表单时只刷新数据不刷新页面的办法,谢谢!

  • 写回答

9条回答 默认 最新

  • 最努力奔跑 2017-02-27 04:03
    关注
    
    ```<!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=gb2312" />
    <title>计算器</title>
    <script type="text/javascript">
    //document.getElementById("sub1").submit();
    function setval(){
    var gd = document.getElementById("gaodu").value;
    var kd = document.getElementById("kuandu").value;
    var sl = document.getElementById("shuliang").value;
    if(gd=="")
    document.getElementById("gaodu").value=1600;
    if(kd=="")
    document.getElementById("kuandu").value=600;
    if(sl=="")
    document.getElementById("shuliang").value=1;
    }
    </script>
    </head>    
    <body onload="setval()">  
    <form action="" id="sub1">  
    <input name="gaodu" type="text" id="gaodu"  value=""> 
    <input name="kuandu" type="text" id="kuandu"  value="">
    <input name="shuliang" type="text" id="shuliang"  value=""> 
    <input name="sub" id="sub" type="button"  value="计算">
    </form>
    <table width="200" border="1">
      <tr>
        <td class="val1">&nbsp;</td>
        <td class="val2">&nbsp;</td>
        <td class="val3">&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <script src="http://static.blog.csdn.net/scripts/jquery.js"></script>
      <script>
        $('#sub').click(function(){
          var gaodu = $('#gaodu').val();
          var kuandu = $('#kuandu').val();
          var shuliang = $('#shuliang').val();
          //和
          var he = parseInt(shuliang)*(parseInt(gaodu)+parseInt(kuandu));
          //差
          var cha = parseInt(shuliang)*(parseInt(gaodu)-parseInt(kuandu));
          //赋值
          $('.val1').html(he);
          $('.val2').html(cha);
          $('.val3').html(shuliang);
        })
      </script>
    </table>
    </body>  
    </html>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥15 方程如何编到C语言!
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了