jQuery - 追加ajax结果

时间:2017-01-30 06:45:13

标签: php jquery ajax

当我从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):

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

我该如何解决这个问题?

谢谢!

4 个答案:

答案 0 :(得分: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);
      }

  });
});

答案 1 :(得分:1)

试试这段代码

&#13;
&#13;
$.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);
          }

      });
    });
&#13;
&#13;
&#13;

答案 2 :(得分: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);
        });
      }

答案 3 :(得分: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>