从JSON获取id并在点击

时间:2015-12-11 05:29:24

标签: javascript jquery json ajax

var maker = new function () {
        count = 1;
        this.load = function () {
            console.log(count + "jdlksa")
            $.ajax({
                'async': true,
                method: "GET",
                'global': false,
                'url': '{/literal}{$siteconfig->skin_url}data/searchList' + count + '.json{literal}',
                'dataType': 'json',
                'complete': function (xhr, status) {
                    //console.log(xhr.responseText);
                    var makers = $.parseJSON(xhr.responseText);
                    //console.log(makers);
                    var makersContainer = $("#search-data");
                    $.each(makers, function (i) {
                        //console.log(this.name);
                        if (this.type === 'university') {
                            $('<div class="col-md-12 search-content"><h2 class="tab-head">' + this.name + '</h2><hr><p class="tab-content">' + this.Location + '<br/>' + this.Courses + '</p></div>').appendTo(makersContainer);
                        }
                        else {
                            $('<div class="col-md-6 college" ><div class="search-content "><h2 class="tab-head">' + this.name + '</h2><hr><p class="tab-content">' + this.Location + '<br/>' + this.Courses + '</p></div></div>').appendTo(makersContainer).click(function() {
 maker.loadCourses(this.id);
});
                        }
                        if (i === 4) {
                            return false;
                        }

                    });
                    //$('<div class="col-md-12"><button class="btn my-btn center-block" style="background-color:#888888; color:#fff;border:1px solid;">View More</button></div>').appendTo(makersContainer).click(maker.loadAll);
                }
            });
        };

        this.loadAll = function () {
            count = count + 1;
            console.log(count);
            maker.load();
        };

        this.loadCourses = function (id) {
            console.log(id);
            $.ajax({
                'async': true,
                method: "GET",
                'global': false,
                'url': '{/literal}{$siteconfig->skin_url}data/courseList.json{literal}',
                'dataType': 'json',
                'success': function (data) {
                    console.log(data.courseDetails[0].name);
                    //var courses = $.parseJSON(data.courseDetails);
                    //console.log("cousrseJSON"+courses);
                    /*var coursesContainer = $("#search-data");
                    $.each(courses, function (i) {
                        if(data.courseDetails[i].name === "Punjab college")
                            //$('<div class="col-sm-3"><div class="search-content"><h2 class="tab-head">' +this.Courses+ '</h2><hr><p class="tab-content">' + this.details +' </p></div></div>').appendTo(coursesContainer);
                            {

                            }
                    });*/

                }
            });
        };

    };

我在加载函数中获取JSON并通过它创建HTML。当我点击一个有一个班级&#34;大学&#34;另一个ajax呼叫启动。现在点击这个,我想从JSON获取id,并在点击时将其传递给另一个ajax调用。

任何类型的帮助和建议都会非常有用。

0 个答案:

没有答案