简体   繁体   English

Javascript jQuery无法刷新div

[英]Javascript jquery not refreshing the div

I have this code HTML 我有这段代码HTML

<form id="myForm" method="post" onsubmit="return updateValues(this)">
    <input id="id" type="hidden" value="<?php echo $row[0]; ?>" />
    <input type="text" id="dd" value="<?php echo $row[1];?>" />
    <input type="text" id="da" value="<?php echo $row[2];?>" />       
    <input type="text" id="db" value="<?php echo $row[3];?>" />
    <input type="submit" id="submit" value="Save Changes" />
</form>

<div id="successNote"></div>

and using javascript to pass the values to php file to perform the update. 并使用javascript将值传递到php文件以执行更新。

function updateValues(thisForm)
{
   if (thisForm == "")
   {
      document.getElementById("successNote").innerHTML = "please fill form.";
      return;
   } 
   var id = document.getElementById('id').value;
   var dd = document.getElementById('dd').value;
   var da = document.getElementById('da').value;
   var db = document.getElementById('db').value;
   var str = 'id='+id+'&dd='+dd+'&da='+da+'&db='+db;

   if (window.XMLHttpRequest)
   {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest();
   }
   else
   {
       // code for IE6, IE5
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
   xmlhttp.onreadystatechange=function()
   {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
        document.getElementById("successNote").innerHTML = "Form has been updated";
        alert('Form has been updated successfully!');
      }
    }   
   xmlhttp.open("GET","update_myForm.php?"+str,true);
   xmlhttp.send();
}

My javascript code is passing the values to php file (PHP is updating these values fine), however, when it return to the html page, it shows the alert and when press ok it is refreshing the whole page. 我的JavaScript代码将这些值传递给php文件(PHP很好地更新了这些值),但是,当它返回html页面时,它会显示警报,而在按下ok时,它将刷新整个页面。 I want it to refresh only the div not the whole page. 我希望它仅刷新div而不刷新整个页面。 I have no idea what I am doing wrong, any suggestion will be appreciatted. 我不知道我在做什么错,任何建议都会受到赞赏。

You missed the return false in your function: 您错过了函数中的return false

function updateValues(thisForm)
{
   // your code...
   xmlhttp.send();
   return false;
}

Can you try this, 你可以试试这个吗

        <form id="myForm" method="post" onsubmit="return updateValues(this)">
            <input id="id" type="hidden" value="<?php echo $row[0]; ?>" />
            <input type="text" id="dd" value="<?php echo $row[1];?>" />
            <input type="text" id="da" value="<?php echo $row[2];?>" />       
            <input type="text" id="db" value="<?php echo $row[3];?>" />
            <input type="submit" id="submit" value="Save Changes" />
        </form>

        <div id="successNote"></div>

        <script>

        function updateValues(thisForm)
        {

           if (thisForm == "")
           {
              document.getElementById("successNote").innerHTML = "please fill form.";
              return false;
           } 
           var id = document.getElementById('id').value;
           var dd = document.getElementById('dd').value;
           var da = document.getElementById('da').value;
           var db = document.getElementById('db').value;
           var str = 'id='+id+'&dd='+dd+'&da='+da+'&db='+db;

           if (window.XMLHttpRequest)
           {
              // code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp = new XMLHttpRequest();
           }
           else
           {
               // code for IE6, IE5
               xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
           }
           xmlhttp.onreadystatechange=function()
           {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
              {
                document.getElementById("successNote").innerHTML = "Form has been updated";
                alert('Form has been updated successfully!');
              }
            }   
           xmlhttp.open("GET","update_myForm.php?"+str,true);
           xmlhttp.send();

           return false;
        }


        </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM