duanmei1946 2016-07-27 10:03
浏览 32
已采纳

基于内容的JQuery排序 - 不工作 -

I have a multiple Divs that I want to sort using the JQuery sort function based on the product name attribute inside the product. However, when I click to run the function, all the divs will suddenly disappear, when I inspected to see the errors, there was no error displayed.

Kindly help me know what's the issue.

Here's my JQuery function and call:

<script type="text/javascript">
    $(document).ready(function(){
        $divs = $("div.section-item");
        $("#asc").on("click", function(){
            var alphabetically = $divs.sort(function(a, b){
                return $(a).find(".name").text() > $(b).find("name").text();
            });
            $("#section-item").html(alphabetically);           
        });
    });
</script>

This is the container with all the Divs:

<div id="block-section-item">
<div id="bdi-button">
    <button type="submit" id="asc" class="rec">Nom</button>
</div>
<div id="section-item">
    <?php
    include 'Include/__Class/Cls_Product.php';
    $product = new ClsProduct($bdd);
    $products = $product->DisplayAllProducts();

    foreach( $products as $row){
        echo'<div class="section-items">
                <span class="section-items-img">
                    <img src="Image/clio-full.jpg">
                </span>
                <span class="section-items-bottom">
                <span class="section-item-text">';
                echo '<h4 class="name">'.$row['Product_name'].'</h4>';
                echo '<h4 class="price">'.$row['Product_price'].'</h4>';
        echo'<span class="section-item-icon">
        <span class="voir-item"><a href="'.$_SERVER['PHP_SELF'].'?id='.$row["Product_id"].'"></a></span>
        <span class="middle-item"></span>
        <span class="nakrih-item"><a href="'.$_SERVER['PHP_SELF'].'?id='.$row["Product_id"].'"></a></span>
        </span>
        </span>';
        echo'</div>';
    }
    ?>                            
</div>
</div>
  • 写回答

1条回答 默认 最新

  • dqkkrt8860 2016-07-27 10:13
    关注

    Try that:

    HTML:

    <div id="block-section-item">
        <div id="bdi-button">
            <button type="submit" id="asc" class="rec">Nom</button>
        </div>
        <div id="section-item">
            <div class="section-items">
                <span class="section-items-img">
                    <img src="Chrysanthemum.gif">
                </span>
                <span class="section-items-bottom">
                    <span class="section-item-text"><h4 class="name">Product_name2</h4><h4 class="price">22</h4><span class="section-item-icon">
                            <span class="voir-item"><a href="/test/index.php?id=2"></a></span>
                            <span class="middle-item"></span>
                            <span class="nakrih-item"><a href="/test/index.php?id=2"></a></span>
                        </span>
                    </span></span></div><div class="section-items">
                <span class="section-items-img">
                    <img src="Chrysanthemum.gif">
                </span>
                <span class="section-items-bottom">
                    <span class="section-item-text"><h4 class="name">Product_name1</h4><h4 class="price">11</h4><span class="section-item-icon">
                            <span class="voir-item"><a href="/test/index.php?id=1"></a></span>
                            <span class="middle-item"></span>
                            <span class="nakrih-item"><a href="/test/index.php?id=1"></a></span>
                        </span>
                    </span></span></div><div class="section-items">
                <span class="section-items-img">
                    <img src="Chrysanthemum.gif">
                </span>
                <span class="section-items-bottom">
                    <span class="section-item-text"><h4 class="name">Product_name3</h4><h4 class="price">33</h4><span class="section-item-icon">
                            <span class="voir-item"><a href="/test/index.php?id=3"></a></span>
                            <span class="middle-item"></span>
                            <span class="nakrih-item"><a href="/test/index.php?id=3"></a></span>
                        </span>
                    </span></span></div>                            
        </div>
    </div>
    

    Javascript:

    $(document).ready(function () {
        $divs = $(".section-items");
        $("#asc").on("click", function () {
    
            var alphabetically = $divs.sort(function (a, b) {
                return $(a).find("[class='name']").text() > $(b).find("[class='name']").text();
            });
            $("#section-item").html(alphabetically);
        });
    });
    

    jsfiddle:

    https://jsfiddle.net/bs49L18k/2/

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

报告相同问题?

悬赏问题

  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件