Javascript 函数在从不同形式调用时表现不同

Javascript 函数在从不同形式调用时表现不同,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我正在使用AJAX在我的服务器上进行调用,根据调用的位置,函数的行为有所不同。下面是一段有问题的代码: <html> <body> <script> function showHint(str) { var xmlhttp; alert("call made"); if (str.length==0) { document.getElementById(

我正在使用AJAX在我的服务器上进行调用,根据调用的位置,函数的行为有所不同。下面是一段有问题的代码:

<html>
<body>
<script>
    function showHint(str)
    {
        var xmlhttp;
        alert("call made"); 
        if (str.length==0)
          { 
          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;
            alert(xmlhttp.responseText);
            }
          }
        xmlhttp.open("GET","myAjax.php?q="+str,true);
        xmlhttp.send();
    }
</script>
<form action=""> 
First name: <input type="text" id="txt1" onkeyup="showHint('hey')" /> 
</form>
<p>Suggestions: <span id="txtHint"></span></p> 

<tr><td><a href=/upload/actionscript.xml>actionscript.xml</a></td><td>0.03 MB</td><td>June 17 2014 11:29:29</td><td> <a href='' onclick="showHint('hey')" /> <img src="images/trash.png" alt="Delete" width="25" height="25"></a></td><tr>
</body>
</html>

函数showHint(str)
{
var-xmlhttp;
警报(“发出呼叫”);
如果(str.length==0)
{ 
document.getElementById(“txtHint”).innerHTML=“”;
返回;
}
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
警报(xmlhttp.responseText);
}
}
open(“GET”,“myAjax.php?q=“+str,true”);
xmlhttp.send();
}
名字:
建议:

0.03 MB2014年6月17日11:29:29
因此,当我在文本字段中键入某个内容时,一切都正常,而且应该这样做,它会被认为是两个警报。 当我点击href时,唯一弹出的警报是“call Make”。删除
中的正斜杠(/)的原因是什么

我已经设法解决了这个问题。感谢Jay Blachard告诉我有关console.log()的信息,这非常有帮助! 我没有得到我想要的响应的原因是因为我引用了“”,这似乎会刷新页面的背景或其他内容。我已将代码更改为:

<a href='#' onclick="showHint('hey')" /> 
     <img src="images/trash.png" alt="Delete" width="25" height="25">
</a>


而且一切都按预期运行

A.)不要使用警报进行故障排除。它们会停止您的JavaScript,并可能使您的结果无法预测。2.)使用
console.log()
。您的
链接是否包含
?为什么单击
调用该函数?没有分配事件处理程序。
<a href='#' onclick="showHint('hey')" /> 
     <img src="images/trash.png" alt="Delete" width="25" height="25">
</a>