Php jQuery-追加ajax结果

Php jQuery-追加ajax结果,php,jquery,ajax,Php,Jquery,Ajax,当我从ajax.php得到一个结果时,我得到了正确的结果,但如果我有两个或更多的结果,我就看不到结果 <script type="text/javascript"> $(document).ready(function() { $('#txt').bind('propertychange keyup input paste',function() { $('div#text-container').html('');

当我从ajax.php得到一个结果时,我得到了正确的结果,但如果我有两个或更多的结果,我就看不到结果

<script type="text/javascript">
     $(document).ready(function() {
         $('#txt').bind('propertychange keyup input paste',function() {

             $('div#text-container').html('');
             var word = $('input#txt').val();

             $.ajax({ 
                 type: 'GET', 
                 url: 'ajax.php', 
                 data: { word: word }, 
                 dataType: 'json',
                 success: function (data) { 
                     if (data.text) {
                         var result = "<strong>" + data.word + '</strong> - ' + data.text
                     } else {
                         var result = "<strong>" + data.word + '</strong> - ' + "not found"
                     }
                     $('div#text-container').append(result);
                 }
             });
         });
    });
</script>
我怎样才能解决这个问题

谢谢

试试这段代码

$.ajax({
键入:“GET”,
url:'ajax.php',
数据:{word:word},
数据类型:“json”,
成功:功能(数据){
如果(data.length>0){
对于(i=0;i
success:function(data){
$。每个(数据、函数(行){
if(row.text){
var result=“”+row.word+”-“+row.text
}否则{
var result=“”+row.word+”-“+”未找到
}
$('div#text container')。追加(结果);
});
}
在ajax部分

  var result="";
   $.ajax({ 
      type: 'GET', 
      url: 'ajax.php', 
      data: { word: word }, 
      dataType: 'json',
      success: function (data) { 
        if (data.length > 0) {
         $.each(data, function(i, item) {
           result += "<strong>" + data[i].word + '</strong> - ' + data[i].text;
           });​
        } else {
           result += "<strong>" + data.word + '</strong> - ' + "not found"
        }
        $('div#text-container').append(result);
      }

  });
});
var结果=”;
$.ajax({
键入:“GET”,
url:'ajax.php',
数据:{word:word},
数据类型:“json”,
成功:函数(数据){
如果(data.length>0){
$。每个(数据、功能(i、项){
结果+=“”+数据[i]。word+'-'+数据[i]。文本;
});​
}否则{
结果+=“”+data.word+”-“+”未找到
}
$('div#text container')。追加(结果);
}
});
});

$(文档).ready(函数(){
$('#txt').bind('propertychange keyup input paste',function(){
$('div#text container').html(“”);
var word=$('input#txt').val();
$.ajax({
键入:“GET”,
url:'ajax.php',
数据:{word:word},
数据类型:“json”,
成功:函数(数据){
var obj=jQuery.parseJSON(数据);
var结果=”;
对于(i=0;i”+obj[i]。word+'-'+obj[i]。文本
}否则{
结果=“”+obj[i]。找不到word+”-“+”
}
}
$('div#text container')。追加(结果);
}
});
});
});

您可以使用json编码。确切地说,您需要使用foreach循环。这一个更准确。+1.为什么要将结果初始化3次?
success: function (data) { 
       $.each( data, function(row){

        if (row.text) {

          var result = "<strong>" + row.word + '</strong> - ' + row.text
        } else {
          var result = "<strong>" + row.word + '</strong> - ' + "not found"
        }
        $('div#text-container').append(result);
        });
      }
  var result="";
   $.ajax({ 
      type: 'GET', 
      url: 'ajax.php', 
      data: { word: word }, 
      dataType: 'json',
      success: function (data) { 
        if (data.length > 0) {
         $.each(data, function(i, item) {
           result += "<strong>" + data[i].word + '</strong> - ' + data[i].text;
           });​
        } else {
           result += "<strong>" + data.word + '</strong> - ' + "not found"
        }
        $('div#text-container').append(result);
      }

  });
});