1
排序標籤我有這樣的HTML與屬性
<td colspan="4" class="item"> DERBY LINE, VT 05830<br>
<label for="" data-sort="16.26">UPS Ground $16.26</label><br>
<label for="" data-sort="27.57">UPS 3 Day Select® $27.57</label><br>
<label for="" data-sort="33.87">UPS 2nd Day Air® $33.87</label><br>
<label for="" data-sort="72.82">UPS Next Day Air $72.82</label><br>
<label for="" data-sort="7.99">USPS Priority $7.99</label><br>
<label for="" data-sort="9.61">FEDEX SmartPost® FedEx Delivered Via USPS $9.61</label><br>
<input type="button" name="Button5" id="Button5" onmouseout="this.className='btn'" onmouseover="this.className='btn_over'" onclick="document.shipquote.action='shipquote.asp?action=clean';document.shipquote.submit();" value="Clear" class="btn">
</td>
,我需要它的「數據排序」屬性是排序,現在我使用 this element sorter 但是當我運行的腳本它返回這個
<td colspan="4" class="item"> DERBY LINE, VT 05830<br>
<label for="" data-sort="16.26">UPS Ground $16.26</label><br>
<label for="" data-sort="27.57">UPS 3 Day Select® $27.57</label><br>
<label for="" data-sort="33.87">UPS 2nd Day Air® $33.87</label><br>
<label for="" data-sort="7.99">USPS Priority $7.99</label><br>
<label for="" data-sort="72.82">UPS Next Day Air $72.82</label><br>
<label for="" data-sort="9.61">FEDEX SmartPost® FedEx Delivered Via USPS $9.61</label><br>
<input type="button" name="Button5" id="Button5" onmouseout="this.className='btn'" onmouseover="this.className='btn_over'" onclick="document.shipquote.action='shipquote.asp?action=clean';document.shipquote.submit();" value="Clear" class="btn">
</td>
你可以看到它運行,但由於某種原因,它只得到attr的第一個數字。我正在使用的呼叫如下:
$('.item label').sortElements(function(a, b){
return $(a).attr("data-sort") > $(b).attr("data-sort") ? 1 : -1;
});
我在做什麼錯?
你是對的,它比較字符串:' 「23」> 「3」 ===假', –
感謝你們所有人,這兩個解決方案工作.. –
@ Blazemonger - 我沒有閱讀'.data()'緊密,但它確實試圖解釋數據類型。 「每一次嘗試都會將字符串轉換爲JavaScript值(這包括布爾值,數字,對象,數組和空值),否則會將其保留爲字符串。」真棒,感謝信息=) – Chase