2013-07-22 49 views
0

解決濾波器的編號範圍功能問題

從以前的文章中我沒有和用的一些建議,我已經成功地寫在技術上應該做的事情需要我把它給函數的幫助。但結果並不完全正確。

我試圖標記添加到JS解釋是怎麼回事,但要解釋,我有div的類似如下:

<div class="section-link"> 
    <div class="price"> £59.99</div> 
</div> 
<div class="section-link"> 
    <div class="price"> £259.99</div> 
</div> 

我想具有的功能隱藏所有這些div的只有表演如果價格在給定價格範圍內的話。

我傳遞給函數的數據是:£0.01 - £59.99£60.00 - £159.99£160.00 - £500.00

使用警報的一切工作正常,但是當它到達該濾波器的if語句是不過濾應該如何。

任何幫助表示讚賞

的JS:

function price(string){ // passing in the strings as £0.01 - £59.99 and £60.00 - £159.99 etc 
    $('.section-link').hide(); // hide all section-link div's' 
    var range = string.replace(/\u00A3/g, ''); // strip pound sign's from range 
    var rangearray = range.split("-"); // split into 2 value arrays 
    lowarray = rangearray[0].toString(); // get low value as string 
    higharray = rangearray[1].toString(); // get high value as string 
    lowvalue = lowarray.replace(/ /g,''); // strip spaces 
    highvalue = higharray.replace(/ /g,''); // strip spaces 

    alert(lowvalue); // testing low value string (is alerting right) - 0.01 
    alert(highvalue); // testing high value string (is alerting right) - 59.99 

    $(".price").filter(function(){ //do a filter for all div's with the class of price 
     var divprice = $(this).text().replace(/\u00A3/g, ''); // strip pound sign from price value 
     var maindivprice = divprice.replace(/ /g,''); // strip spaces from price value 
     if (maindivprice >= lowvalue && maindivprice <= highvalue) { 
      alert(maindivprice); // alerting to see what prices it is saying are between the range (these are showing all the prices and not only ones between the range) 
      $(this).parent().show(); // show this parents div 
     } // filter to see if this price is in the price range 
    }); 
} 

是它可能是與小數點?

+0

感謝這個做的工作 –

+1

比較浮點值,我認爲你錯過了'。 '在parent()之前,對吧? – Akheloes

+0

您的快速,我希望我及時修復它 –

回答

1

嘗試在你的電話號碼使用可變parseFloat,如果這是一個字符串,那麼它正試圖爲字符串值

lowvalue = parseFloat(lowarray.replace(/ /g,'')); // strip spaces 
highvalue = parseFloat(higharray.replace(/ /g,'')); // strip spaces 
var maindivprice = parseFloat(divprice.replace(/ /g,'')); // strip spaces from price value