我现在尝试过滤两个选项,但稍后会添加第三个,也许是第四个(例如:现在我在价格和评论之间进行过滤,但希望在列表中添加更多价格和还有另一个过滤器类别,如“评级”,由 3、4 或 5 颗星组成)。
使用我在下面列出的逻辑可以正常工作,但我觉得它会变得非常冗长和复杂(并且不必要)。我知道有一种方法可以重构代码我只是想知道最好的方法是什么?HTML:
<select class="deals">
<option value="deals-all">All deals</option>
<option value="50">$50</option>
<option value="25">$25</option>
</select>
<select class="reviews">
<option value="reviews-all">All reviews</option>
<option value="reviews-positive">Positive reviews</option>
<option value="reviews-negative">Negative reviews</option>
</select>
jQuery
$('.reviews, .deals').change(function() {
var reviewsVal = $('.reviews :selected').val();
var dealsVal = $('.deals :selected').val();
if((reviewsVal == 'reviews-all') && (dealsVal == 'deals-all')) {
$('.review-positive').show();
$('.review-negative').show();
$('.deals-25').show();
$('.deals-50').show();
}
else if((dealsVal == '50') && (reviewsVal == 'reviews-positive')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').show();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == '50') && (reviewsVal == 'reviews-negative')) {
$('.review-negative.deals-50').show();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == '25') && (reviewsVal == 'reviews-positive')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').show();
}
else if((dealsVal == '25') && (reviewsVal == 'reviews-negative')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').show();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == 'deals-all') && (reviewsVal == 'reviews-positive')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').show();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').show();
}
else if((dealsVal == 'deals-all') && (reviewsVal == 'reviews-negative')) {
$('.review-negative.deals-50').show();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').show();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == '50') && (reviewsVal == 'reviews-all')) {
$('.review-negative.deals-50').show();
$('.review-positive.deals-50').show();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == '25') && (reviewsVal == 'reviews-all')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').show();
$('.review-positive.deals-25').show();
}
else {
$('.review-positive').show();
$('.review-negative').show();
$('.deals-25').show();
$('.deals-50').show();
}
});
$('.reviews-positive').click(function() {
$('.review-negative').hide();
$('.review-positive').show();
});
$('.reviews-negative').click(function() {
$('.review-positive').hide();
$('.review-negative').show();
});
});
希望您能看到我的目标,感谢您的任何意见。
*编辑:jsfiddle:http: //jsfiddle.net/TXywp/1/