简体   繁体   English

使用 AJAX、JQuery 和 HTML 显示/隐藏数据

[英]Show/Hide data using AJAX, JQuery and HTML

As part of a "Review" section, I have a button to read all reviews, with only a few on the page at the moment.作为“评论”部分的一部分,我有一个按钮可以阅读所有评论,目前页面上只有少数评论。 I want the button to show the data from a.json file using AJAX and JQuery.我希望按钮使用 AJAX 和 JQuery 显示来自 a.json 文件的数据。

HTML.. HTML..

    <section class="prod-review">
        <section class="review">
            <img src="images/review.jpg" alt="icon-1"/>
            <h4><span class="rating">*****</span><br/><span class="review-header">Great!!!</span></h4>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
        </section>
            <hr/>
        <button id='button'><a href="#" class="show">SHOW REVIEWS</a></button>
        <button id='button'><a href="#" class="hide">HIDE REVIEWS</a></button>
    </section>

JavaScript... JavaScript...

    $(document).on('ready', function() {
    
        $('#button').on('click', function() {

            $.ajax('reviews.json', {
                type: 'GET',
                dataType: 'json',
                success: function(url) {
                    for(var i = 0; i < url.length; i++) {
                        $('.prod-review').append('<dd>' + url[i].id, url[i].product_id, url[i].nickname, url[i].review, url[i].rating + '</dd>');
                
                    }
                }
            });

            $('a.show').on('click', function(event) {
                event.preventDefault();
                var link = $(this);
                var list_item = link;
                var reviews = list_item.find('reviews.json');
                reviews.show();
                $(this).show();
                $(this).hide();
            });

            $('a.hide').on('click', function(e) {
                e.preventDefault();
                $(this).find('reviews.json').hide();
                $(this).show();
                $(this).hide();
            });

        });
    
    });

JSON Data.. JSON 数据..

[{"id":"1","product_id":"b-o","nickname":"Joe","review":"Great :3","rating":"5"}]

With this code, the data is appending to the page under the button when it is clicked, although showing all as one string, like this..使用此代码,当单击按钮时,数据将附加到按钮下的页面,尽管全部显示为一个字符串,如下所示..

499 b-oJoeGreat:35第499章:35

I am relatively new to JS and completely new to AJAX and JQuery, and I want to get an understanding of how it works.我对 JS 比较陌生,对 AJAX 和 JQuery 完全陌生,我想了解它是如何工作的。

Basically I need the data in the JSON file to appear under the button when clicked and then hide when the button is clicked again.基本上我需要 JSON 文件中的数据在单击时出现在按钮下,然后在再次单击按钮时隐藏。 Preferably with the data split as well, so it doesn't all appear on one line.最好也将数据拆分,因此它不会全部出现在一行上。 I have spent a couple of days trying different things and this is the closest I got.我花了几天时间尝试不同的东西,这是我得到的最接近的。

Any help would be much appreciated.任何帮助将非常感激。

Edit.编辑。 I split the objects using some simple code.我使用一些简单的代码拆分对象。 It now appears how I want it to on screen when I click the button, but it won't hide it.现在,当我单击按钮时,它会显示我希望它在屏幕上显示的方式,但它不会隐藏它。 It also appends the data everytime I click the button.每次我单击按钮时,它也会附加数据。

JavaScript.. JavaScript..

$(document).on('ready', function() {
    
    $('#button').on('click', function() {
    $.ajax('reviews.json', {
        type: 'GET',
        dataType: 'json',
        success: function(data) {
            for(var i = 0; i < data.length; i++) {
                $('.prod-review').append('<dd>' + 'User ID:' + ' ' + data[i].id + '</dd>');
                $('.prod-review').append('<dd>' + 'Product:' + ' ' + data[i].product_id + '</dd>');
                $('.prod-review').append('<dd>' + 'Nickname:' + ' ' + data[i].nickname + '</dd>');
                $('.prod-review').append('<dd>' + 'Review:' + ' ' + data[i].review + '</dd>');
                $('.prod-review').append('<dd>' + 'Rating:' + ' ' + data[i].rating + '</dd>' + '<br/>');
            }
        }
    });
    });

    $('a.show').on('click', function(event) {
        event.preventDefault();
        $(this).find('reviews.json').show();
      });

    $('a.hide').on('click', function(e) {
        e.preventDefault();
        $(this).find('reviews.json').hide();
    });
});

I see your problem now is changing visibility of the review.我看到你现在的问题是改变评论的可见性。 Wrap the review inside div then use .toggle() to show or hide it.将评论包裹在 div 中,然后使用.toggle()显示或隐藏它。

 $(document).on('ready', function() { $('#button').on('click', function() { var divReviews = $('#moreReviews'); var btnReview = $(this); if (divReviews.text().length) { // reviews already downloaded, change button text // then show or hide it btnReview.text(btnReview.text() == 'SHOW REVIEWS'? 'HIDE REVIEWS': 'SHOW REVIEWS'); divReviews.toggle(); return } divReviews.text('Loading ajax content...'); $.ajax({ url: 'https://httpbin.org/get', type: 'GET', dataType: 'json', success: function(data) { divReviews.text(''); // set dummy data data = [{"id":"1","product_id":"bo","nickname":"Joe","review":"Great:3","rating":"5"}]; for (var i = 0; i < data.length; i++) { var dd = '<dd>User ID: ' + data[i].id + '</dd>' + '<dd>Product: ' + data[i].product_id + '</dd>' + '<dd>Nickname: ' + data[i].nickname + '</dd>' + '<dd>Review: ' + data[i].review + '</dd>' + '<dd>Rating: ' + data[i].rating + '</dd>' + '<br/>'; divReviews.append(dd); } btnReview.text('HIDE REVIEWS'); } }) }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <section class="prod-review"> <section class="review"> <img src="images/review.jpg" alt="icon-1" /> <h4><span class="rating">*****</span><br /><span class="review-header">Great,,.</span></h4> <p>Lorem ipsum dolor sit amet: consectetur adipiscing elit; sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> </section> <hr /> <button id="button" style="cursor: pointer;">SHOW REVIEWS</button> <!-- insert ajax content inside this --> <div id="moreReviews"></div> </section>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM