2014-09-12 63 views
0

我已經完成了列表標籤按字母順序排序在javascript/jquery中,當我第二次點擊時,它工作。我的意思是第一次點擊它的工作幾秒鐘(不到一秒),並返回到未排序但第二次點擊後它正常工作。第二次點擊功能正在jquery中排序工作

我的排序功能是

function status() { 
    var mylist = $('ul'); 
    var listitems = mylist.children('li').get(); 
    listitems.sort(function (a, b) { 
     var compA = $(a).find('.Status-For-Sorting').text().toUpperCase(); 
     var compB = $(b).find('.Status-For-Sorting').text().toUpperCase(); 
     return (compA < compB) ? -1 : (compA > compB) ? 1 : 0; 
    }); 
    $.each(listitems, function (idx, itm) { 
     mylist.append(itm); 
    }); 
} 

而且我定義功能deviceready像

document.addEventListener("deviceready", init, false); 

function init() { 
    $().ready(function() { 
     $("#sort-button-status").click(status); 
    } 
} 

***** HTML ****

<a value="sort" id="sort-button-status" class="sort-button-status">status</a> 

告訴我大家在哪裏我錯了這個c頌根據狀態排序正確,但經過第二次點擊..

+0

使用'$(文件)。就緒(函數(){.. 。})(或快捷方式'$(function(){...})')而不是'$()。ready(function(){...})',你似乎在混合這兩個選項列出[在jQuery文檔](http://learn.jquery.com/using-jquery-core/document-ready/)。 – Shai 2014-09-12 12:59:41

回答

0

嘗試使用.appendTo()應用排序列表mylist

function status() 
{ 
    var mylist = $('ul'); 
    var listitems = mylist.children('li').get(); 
    listitems.sort(function(a, b) { 
     var compA = $(a).find('.Status-For-Sorting').text().toUpperCase(); 
     var compB = $(b).find('.Status-For-Sorting').text().toUpperCase(); 
     return (compA < compB) ? -1 : (compA > compB) ? 1 : 0; 
    }).appendTo(mylist); 
} 
+0

嘿** Bhusan **我試過你的代碼,但這是沒有工作..我的意思是我追加在最後的返回值.apendTo(mylist),但此功能不工作; – 2014-09-12 13:05:24

+0

它是'appendTo'而不是'apendTo',請檢查您的拼寫 – 2014-09-12 13:06:56

+0

對不起,我用appendTo(),這是我的錯誤,我寫了apendTo在評論而不是appendTo().. – 2014-09-12 13:31:50

相關問題