【发布时间】: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