【问题标题】:Ajax loader script calls php script twiceAjax 加载程序脚本两次调用 php 脚本
【发布时间】:2013-02-18 20:40:02
【问题描述】:

我在我的 html 页面上使用 ajax 从 db w.o. 加载数据。重新加载并在某些事件上发送电子邮件通知,它的作品完美 同时我使用显示和隐藏 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脚本?

【问题讨论】:

  • 您发布的代码应该行为正确。我可能遗漏了一些东西,但问题应该出在其他地方。确保您没有注册两次监听器。
  • 在我的问题中添加代码

标签: javascript jquery ajax


【解决方案1】:
  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()); });
 });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 2018-05-06
    • 2020-02-28
    相关资源
    最近更新 更多