2010-11-18 24 views
0

如果我有这样的:是否可以通过percentDiscount作为参数?

var percentDiscount; 

function calculate(sale, original) { 

percentDiscount = Math.round(eval(((original-sale)/original) * 100)); 

    return percentDiscount; 
} 

$(document).ready(function() { 

var salecost; 
var originalcost; 
var percent; 
$('.product').each(function() { 

    salecost = $(this).find('.price').html(); 
    salecost = salecost.replace(/[^\d\.]/g, ""); 


    originalcost = $(this).find('.actualprice').html(); 
    if(originalcost == null) return; 
    originalcost = originalcost.replace(/[^\d\.]/g, ""); 

    percentDiscount = calculate(salecost, originalcost); 

    if (69 < percentDiscount && percentDiscount < 101) { 

     $(this).find("#percentoff").html('&gt; 70% off'); 
     $(this).find("#percentoff").addClass('badge70'); 
    } 

    else if (49 < percentDiscount && percentDiscount < 70) { 

     $(this).find("#percentoff").html('&gt; 50% off'); 
     $(this).find("#percentoff").addClass('badge50'); 
    } 

    else if (29 < percentDiscount && percentDiscount < 50) { 

     $(this).find("#percentoff").html('&gt; 30% off'); 
     $(this).find("#percentoff").addClass('badge30'); 
    } 


    else if (19 < percentDiscount && percentDiscount < 30) { 

     $(this).find("#percentoff").html('&gt; 20% off'); 
     $(this).find("#percentoff").addClass('badge30'); 
    } 


}); 
}); 

是否有可能通过percentDiscount,这样我可以排序的页面上的折扣?例如,按折扣百分比进行排序:[全部] [20%] [30%] [50%] [70%],然后按折扣显示每个项目的计算结果?

如果可能,请举例说明它应该如何编码或排序?我在这方面寻找可能的答案,但无济于事。我所看到的最多的是xxx.sort(),但它不适用于百分比折扣...

另一个问题 - 我想知道如何通过链接进行排序,以适应百分比折扣以及。那么如何跳过没有出售的物品?

+0

你必须更清楚你的第二个问题 – sirrocco 2010-11-19 06:09:28

回答

1

我会尝试这样的:

// create an array of dom elements - this assumes a structure like : 
// <div class='myStuff'> 
// <span class='product'></span> 
// <span class='product'></span> 
// </div> 
// and the spans get sorted inside the .myStuff div 

var products = []; 
$(".product").each(function(){ 
    products.push(this); 
}); 

products.sort(function(left,right){ 
    //get left percentDiscount 
    //get right percentDiscount 

    if(leftDiscount < rightDiscount) 
    return -1; 
    if(leftDiscount > rightDiscount) 
    return 1; 
    return 0; 
}); 

// then clear the original .products from .myStuff div and re-add them from the products array. 

对于//获得百分之左:

salecost = $(left).find('.price').html(); 
salecost = salecost.replace(/[^\d\.]/g, ""); 


originalcost = $(left).find('.actualprice').html(); 
if(originalcost == null) return; 
originalcost = originalcost.replace(/[^\d\.]/g, ""); 

percentDiscount = calculate(salecost, originalcost); 

与同为正确的 - 这些都只是HTML元素。

+0

等待我有点困惑。我们如何获得百分比折扣或百分比折扣?你有没有提到上面的if语句? – joe 2010-11-18 09:57:05

+1

我已经更新了我的答案。 – sirrocco 2010-11-18 10:55:05

+0

aha ..非常感谢 – joe 2010-11-19 04:20:23

相关问题