簡體   English   中英

Ajax加載程序腳本兩次調用php腳本

[英]Ajax loader script calls php script twice

我在我的html頁面上使用ajax從db wo重新加載數據並在某些事件中發送電子郵件通知,它的工作原理完美,同時我使用了顯示和隱藏ajax加載器圖像的功能,但是它兩次調用了我的php腳本,因此每次兩次發送電子郵件的時間

<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.js"></script>

<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","../getuser.php?q="+str,true);
xmlhttp.send();
}

// ajax loader image

  function getuser(str){
     $.ajax({
      url: "http://site.com/getuser.php?q="+str,
      beforeSend: function (XMLHttpRequest)
      {
         $("#loading").show();
      }
    }).done(function ( data ) {
     $("#txtHint").html(data);
     $("#loading").hide(); });
  }
$(document).ready(function(){
  $("select[name='users']").change(function () {
   getuser($("option:selected", this).val()); });
   });

</script>

如何改進此功能以一次調用php腳本?

  function getuser(str){
 $.ajax({
  url: "http://site.com/script.php?q="+str,
  beforeSend: function (XMLHttpRequest)
  {
     $("#loading").show();
  }
}).done(function ( data ) {
 //     $("#txtHint").html(data);
 $("#loading").hide(); });
}
$(document).ready(function(){
$("select[name='users']").bind('change',function () {
 getuser($("option:selected", this).val()); });
 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM