2013-02-18 28 views
0

我在我的html頁面上使用ajax從db w.o加載數據。重裝和某些事件發送電子郵件通知,其作品完美 並在同一時間,我使用的功能顯示和隱藏阿賈克斯裝載機的圖像,但它會調用我的PHP腳本兩次,每次它發送電子郵件兩次Ajax loader腳本調用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腳本?

+1

您發佈的代碼應該表現正確。我可能錯過了一些東西,但問題應該在其他地方。確保你沒有註冊一個監聽器兩次。 – sitifensys 2013-02-18 12:14:55

+0

在我的問題中加入代碼 – 2013-02-18 12:40:33

回答

0
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()); }); 
}); 
+0

完全相同,我在問題中添加了整個代碼可能是它的幫助 – 2013-02-18 12:44:26