从不同形式调用时,函数的行为会有所不同

时间:2014-06-19 13:20:44

标签: javascript php jquery html ajax

我正在使用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>

因此,当我在文本字段中输入内容时,一切正常并且应该是,它会被认为是两个警报。 当我点击href时,弹出的唯一警告是&#34;调用&#34;。这是什么原因

2 个答案:

答案 0 :(得分:0)

删除<a /> .. </a>

中的正斜杠(/)
                                     <!--  Remove this slash   -->
<a href='' onclick="showHint('hey')" /> 
     <img src="images/trash.png" alt="Delete" width="25" height="25">
</a>

答案 1 :(得分:0)

我设法解决了这个问题。谢谢Jay Blachard告诉我有关console.log()的信息,这对我有很大的帮助! 我没有得到我正在寻找的响应的原因是因为我正在引用'',这似乎是在后台刷新页面或其他东西。我已将代码更改为:

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

一切都按预期工作