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

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>
    
    
    点赞 打赏 评论
  • blownewbee 2017-02-25 16:09

    不刷新页面就不要用submit,而是ajax方式,jquery里的 $ajax
    http://www.cnblogs.com/johnsneakers/archive/2011/12/06/2278425.html
    http://www.jb51.net/article/65018.htm

    点赞 打赏 评论
  • 仰望星空2010 2017-02-26 02:06

    使用Ajax方式提交。例如可以使用Jquery提供的submit方法提交表单

    点赞 打赏 评论
  • 小啊小木头 2017-02-26 13:07

    ajax,局部刷新,这样就不需要form的submit了

    点赞 打赏 评论
  • qq_18136213 2017-02-27 00:46

    谁能给具体代码吗?看不懂啊。

    点赞 打赏 评论
  • 最努力奔跑 2017-02-27 03:55

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



    计算器 //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; }



    $('#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); })
         
         
         



    点赞 打赏 评论
  • luochengquan 2017-03-12 12:51

    jquery

    点赞 打赏 评论
  • 曼巴童鞋 2017-06-07 03:04

    使用jquery.form.js插件实现表单异步提交 下载地址:

    图片说明

    点赞 打赏 评论
  • 曼巴童鞋 2017-06-07 03:11
    点赞 打赏 评论

相关推荐 更多相似问题