2013-03-10 14 views
0

我希望用戶在文本字段中輸入任何字母,當他/她點擊Find!按鈕時,它會通過我的php文件並找到以該字母開頭的單詞列表(當然這只是一個限制字數)。我想輸出到一個表格,5列,每個單元格包含一個單詞。如何在表中存儲數組數據?

事情是這樣的:

5columns

在我的HTML

<label> 
    Enter any alphabet: 
     <input name="alphabet" type="text" id="alphabet"></label> 
     <input type="button" value="Find!" id="goFind"> 

<table border="1" id="output"> 
</table> 

和Javascript:

$(document).ready(function() { 
    $.ajaxSetup({ 
    cache: false 
    }); 

    var bunchOfWords = function (data) { 
    var listOfWords = ""; 

    if(!Array.isArray(data)) { 
     return false; 
    } 
    else { 
     for(i = 0; i < data.length; i = +5) { 
     listOfWords += "<tr><td>" + data[i] + "</td>" + 
      "<td>" + data[i] + "</td>" + 
      "<td>" + data[i] + "</td>" + 
      "<td>" + data[i] + "</td>" + 
      "<td>" + data[i] + "</td></tr>"; 
     } 
    } 
    }; 

    $("#goFind").click(function() { 
    var theWord = $("#alphabet").val(); 
    $("#output").html("Loading..."); //gives the user an indication that it's loading 
    $.getJSON("wordslookup.php", "startswith=" + theWord, listOfWords); 
    }); 
}); 

似乎無法弄清楚什麼是錯的。

+0

描述會發生什麼,而不是預期的結果會有幫助...你至少看看你的瀏覽器的開發工具/錯誤控制檯? – DCoder 2013-03-10 06:11:30

回答

0

listOfWords不在$.getJSON的範圍之內也不是它的功能,您應該通過bunchOfWords改爲$.getJSON。把表中的數據簡單地替換當前的內部html,$("#output").html(listOfWords);

相關問題