2011-12-29 66 views
0

我已經與這一半的一天或更多的摔跤。我無法弄清楚什麼是錯的。我剝去了其他的JavaScript函數,php代碼等;我仍然會重新加載一個頁面,而不是一個沒有任何返回的異步調用。 Firebug也不會顯示XHR呼叫。jquery - 阿賈克斯調用只是不工作

jquery加載正常,並且對其他功能正常工作。任何Ajax或jSON調用都不能異步工作。

我甚至將按鍵功能改變爲一個簡單的按鈕點擊處理程序和相同的結果......沒有。

如果我直接加載「findmatch.php」頁面,它會生成正確的結果。

在同一個目錄中的另一個程序毫不費力和完美地使用了大量的ajax和jSON調用。相同的doctype,相同版本的jquery,相同的ajax調用結構....我很困惑。我一直在尋找真正愚蠢的錯誤,因爲這些事情往往是......但沒有運氣。

 <script type=text/javascript> 
     $(document).ready(function() { 

    $('#searchtext').live('keypress',function(e) 

    {  if (e.which == 13)   { 
       mysearch=$("#searchtext").val();   if(mysearch=="")   { 
         alert("Trying typing something in the box, ok?");   }   else   { 
        $.ajax({ 
        url: 'findmatch.php',       
        data: 'x='+mysearch,    
        dataType: 'html', 
        success: function(res)   
        { 
         alert(res);  

         if(res.length>0) 
         { 
          buildit="<table><tr><td>id</td>td><b>Machine Name</b></td></tr>"; 

          buildit=buildit+res+"</table>"; 

          $("#searchresults").html(buildit); 

          $("$searchtext").focus(); 
         } 
         else 
         { 
          $("#searchresults").html("No Results Found"); 
          $("#searchtext").focus(); 
         } 
        } 
        });    } 
          }  }); 


    }); 

    </script> 

</head> 

<body> 


<div class="searchy"> 
<h3>Search for an existing record?</h3> 

<form> 
Enter a Machine Name to search for, such as "bl-psy-srv" or "psy-srv" or something similar. A partial name will return appropriate matches. Hit Enter to submit. 
<br /> 
<input type=text id="searchtext" size=30> 
</form> 

</div> 

<div class="searchy" id="searchresults"> 
</div> 
+0

什麼提琴手交通節目? – asawyer 2011-12-29 21:50:18

回答

1

#searchtext的形式嗎?我認爲回車會觸發表單上的提交操作,而您並未阻止它。嘗試插入一個e.preventDefault(),看看它是怎麼回事。

無關的,你可能想URL編碼mysearch,或更好,但只使用一個對象 - jQuery是足夠聰明,將其轉換成適當的形式:{ x: mysearch }

+0

omg ... e.preventDefault()做了訣竅。小時,我告訴你,小時我搞砸了。 :)我實際上認爲它是這樣的,但也感到「肯定」,如果(e.which == 13){}處理它。 *嘆息* 非常感謝!還有一件事要堅持我的心理jQuery數據庫。 – Steve 2011-12-29 22:06:06