如何在ajax html()中获取子数组 [英] How to get child arrays in ajax html()

查看:40
本文介绍了如何在ajax html()中获取子数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据数组的表单,将使用 html()打印以查看此数据,每个数据都有一个子数组,我需要在我的中获取这些子数组的数据html()以及

I have a form for my array of data will print to view with html() and this data each one has child array, I need to get data of those child arrays in my html() as well

我的数据

外观如何

#代码

HTML

<div class="answerPanel"></div>
<button id="clicks" class="btn btn-primary">Begin</button>

脚本

var index = 0;
$("#clicks").click(function(){
    // timer function
    function timer(seconds, countdownTimer, callback) {
        var days = Math.floor(seconds / 24 / 60 / 60);
        var hoursLeft = Math.floor((seconds) - (days * 86400));
        var hours = Math.floor(hoursLeft / 3600);
        var minutesLeft = Math.floor((hoursLeft) - (hours * 3600));
        var minutes = Math.floor(minutesLeft / 60);
        var remainingSeconds = seconds % 60;
        if (remainingSeconds < 10) {
            remainingSeconds = "0" + remainingSeconds;
        }
        document.getElementById('countdown').innerHTML = hours + ":" + minutes + ":" + remainingSeconds;
        if (seconds == 0) {
            clearInterval(countdownTimer);
            document.getElementById('countdown').innerHTML = "Times Up!";
            $("#clicks").attr("disabled", true);
            $('.answerPanel').html('<div class="text-center text-danger">OH NO! <br> Times Up!</div>');
        } else {
            seconds--;
        }

        //Pass seconds param back to the caller.
        callback(seconds);
    }
    //We pass the countdownTimer param into the timer function as well.
    var countdownTimer = null,
    seconds = data.quizzes[index].quiz_time;
    countdownTimer = setInterval(function() {
        timer(seconds, countdownTimer, function(_seconds){
            seconds = _seconds;
        })
    }, 1000);

    // printing function
    if(typeof  data.quizzes[index] != 'undefined'){
        var row = `<form>
            <div class="row">
                <div class="col-md-12">
                    <div class="pull-left questionTitle">
                        ${data.quizzes[index].question}
                    </div>
                    <div class="pull-right" id="countdown"></div>
                </div>
                <div class="col-md-12">
                    Choice 1
                </div>
                <div class="col-md-12">
                    Choice 2
                </div>
                <div class="col-md-12">
                    Choice (etc.)
                </div>
            </div>
        </form>`;
        $('.answerPanel').html(row);
        index++;
    }
    if(data.quizzes.length > index+1) {
        $("#clicks").html("Next");
    }
    if(data.quizzes.length === index) {
        $("#clicks").html("Finish");
    }
    //end of printing function
});

有什么想法吗?

推荐答案

请在下面查看我的代码

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<body  id="banner">
<ul id="eventCal">
</ul>
<button id="clicks">Click</button>
<script type="text/javascript">
	jQuery(document).ready(function($){

	var quizzes = [{title:'title1',choices:[{choice:'choice1'},{choice:'choice2'}]},
				   {title:'title2',choices:[{choice:'new1'},{choice:'new2'}]},
				   {title:'title3',choices:[{choice:'demo1'},{choice:'demo2'}]}];
	var index   = 0;
	//console.log(quizzes.length)
	$("#clicks").click(function(){
		if(typeof  quizzes[index] != 'undefined'){
			var html = '<li><span>'+quizzes[index].title+'</span></li>';
			if(quizzes[index].choices.length > 0){
			html+='<li class="choises">';
			quizzes[index].choices.forEach((element, index, array) => {
				//console.log(element.title); 
				html+='<ul>';
				html+='<li><span>'+element.choice+'</span></li>';
				html+='</ul>';
			});
			html+='</li>';
			}
			
			
			$("#eventCal").html(html);
			index++;
		}
		if(quizzes.length === index)
			$("#clicks").html("Finish");
		
	})
});

</script>
</body>

这篇关于如何在ajax html()中获取子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆