2017-01-30 79 views
1

当我有一个结果从ajax.php我得到正确的结果,但如果我有两个或两个以上的结果我看不到导致jQuery的 - 追加AJAX结果

<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> 

结果的例子(从阿贾克斯即将) :

[{ 
    "word": "Hell", 
    "text": "Hell" 
}, { 
    "word": "Hello", 
    "text": "Hello" 
}] 

我该如何解决这个问题?

谢谢!

+0

你可以json编码。 – neophyte

+0

正是你需要使用foreach循环 – Sona

回答

2

在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); 
     } 

    }); 
}); 
+0

这一个更准确。 +1。 –

+0

为什么初始化结果3次? – guradio

+0

谢谢!谢谢!!!! –

1

试试这个代码

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

 
     }); 
 
    });

0
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); 
     }); 
     } 
0

<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) { 
 
      var obj = jQuery.parseJSON(data); 
 
      var result = ""; 
 
      for (i = 0; i < obj.length; ++i) { 
 
      if (obj[i].text) { 
 
       result = "<strong>" + obj[i].word + '</strong> - ' + obj[i].text 
 
      } else { 
 
       result = "<strong>" + obj[i].word + '</strong> - ' + "not found" 
 
      } 
 
      } 
 
      $('div#text-container').append(result); 
 
      } 
 

 
     }); 
 
    }); 
 

 
}); 
 
</script>