weixin_33697898 2017-01-30 06:45 采纳率: 0%
浏览 14

jQuery-附加ajax结果

When I have one result from ajax.php I get correct result, but if I have two or more result I can't see result

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

result example(Coming from ajax):

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

How can I solve this problem?

Thanks!

  • 写回答

4条回答 默认 最新

  • 乱世@小熊 2017-01-30 06:48
    关注

    try this code

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

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测