dpr81047 2017-02-24 12:34
浏览 42
已采纳

如何将json数组设置为html表单div。

Here is my first question How to decode / separate php encoded json in jquery.

I am working with same this question code, I can get and set my retrieved array separately but I can't not set it in HTML format. Here I put my code with HTML.This code work with page load. window.onload function(){};. any help appreciate.

var j=0;
var k=0;
if($.trim(passid) != 0)
{
    $.post('retrivedata.php', { passid: passid}, function(data){             
        alert(1);       
        var da = JSON.parse(data);              
        alert(22);
        //alert(text(da.QP_Name));
        $('div#datatable').text(da.QP_Name);
        $('div#datatable1').text(da.Question);

                strtable += '';
                strtable += '<div class="col-lg-12" style="vertical-align: middle; padding-left:50px; padding-top: 25px">';
                strtable += '<span>';                   
                $.each(da, function(i, field)){
                    if(k % 6 === 0){
                        strtable +=
                        strtable += '<div class="col-lg-2" id="'+Que+i+'">';
                        strtable += '<figure class="wp-caption"><div class="col-lg-2"><img class="demo" src="assets/images/ico1.png" alt="2" />';
                        strtable += '<figcaption class="wp-caption-text">'+j+'</figcaption></div></figure>';
                        strtable += '<div>' + da[i]['Q_Id'] + '</div>';
                        strtable += '</div>';
                    }else{
                        strtable += '<div class="col-lg-2" id="'+Que+i+'">';
                        strtable += '<figure class="wp-caption"><div class="col-lg-2"><img class="demo" src="assets/images/ico1.png" alt="2" />';
                        strtable += '<figcaption class="wp-caption-text">'+j+'</figcaption></div></figure>';
                        strtable += '<div>' + da[i]['Q_Id'] + '</div>';
                        strtable += '</div>';
                    }
                    k++; j++;
                }
                strtable += '</span>';
                strtable += '</div>';

                $('div#EnglishLangImageBox').html(strtable);
    });
}
  • 写回答

1条回答 默认 最新

  • dtxa49711 2017-02-24 12:56
    关注

    Your looping is wrong change your code to something like this:

    NOTE: This example usages a sample data which may not match to yours.

      var da=[];
     for(var i=0;i <10;i++){
         da[i]=[];
         da[i]['Q_Id']='Q_Id'+i; 
     }
     var strtable="",k=0,j=0,Que="Text";
     strtable += '<div class="col-lg-12" style="vertical-align: middle; padding-left:50px; padding-top: 25px">';
     strtable += '<span>';    
     $.each(da, function(i, field){
         if(k % 6 === 0){
             strtable +="</span>";//close the opened span
             strtable +="</div>";//close the opened div
             strtable += '<div class="col-lg-12" style="vertical-align: middle; padding-left:50px; padding-top: 25px">';//open new div
             strtable += '<span>';   //open new span
             strtable += '<div class="col-lg-2" id="'+Que+i+'">';
             strtable += '<figure class="wp-caption"><div class="col-lg-2"><img class="demo" src="https://maxcdn.icons8.com/office/PNG/512/Computer_Hardware/mouse_left_click-512.png" alt="2" style="width:32px;height:32px;"/>';
             strtable += '<figcaption class="wp-caption-text">'+j+'</figcaption></div></figure>';
             strtable += '<div>' + da[i]['Q_Id'] + '</div>';
             strtable += '</div>';
         }else{
             strtable += '<div class="col-lg-2" id="'+Que+i+'">';
             strtable += '<figure class="wp-caption"><div class="col-lg-2"><img class="demo" src="https://maxcdn.icons8.com/office/PNG/512/Computer_Hardware/mouse_left_click-512.png" alt="2" style="width:32px;height:32px;" />';
             strtable += '<figcaption class="wp-caption-text">'+j+'</figcaption></div></figure>';
             strtable += '<div>' + da[i]['Q_Id'] + '</div>';
             strtable += '</div>';
         }
         k++; j++;
     });
     strtable += '</span>';
     strtable += '</div>';
    
     $('#EnglishLangImageBox').html(strtable);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="EnglishLangImageBox">
    
     
     </div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行