2016-07-27 60 views
0

我有多個Div,我想使用基於產品內部產品名稱屬性的JQuery排序函數進行排序。但是,當我點擊運行該功能時,所有div將突然消失,當我檢查以查看錯誤時,沒有顯示任何錯誤。JQuery按內容排序Div-Not Working-

請幫助我知道是什麼問題。

這裏是我的jQuery函數和調用:

<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> 

這是所有的DIV容器:

<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

試一下:

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/

+0

非常感謝您的及時回覆,但你能告訴我什麼改變你的HTML文件所做的?因爲JavaScript代碼是相同的並且不起作用。我一直在檢查html文件,我無法找到我和你的區別(在類名和divs編號方面) 這一次它並沒有消失,但訂單很雜亂,效率也不高 – Alladin

+0

用新的代碼 – rad11

+0

我確實做過,我看到你做了幾個更新到你的答案,但是,我看到它的最新更新,它在jsfiddle中爲你工作,但在我的代碼中沒有 – Alladin