using variable with jquery selector

2019-02-26 01:02发布

问题:

I have following lines of code and I want to use visibleoffer variable as selector. How can I concatenate it?

var visibleoffer = '.' + obj.parents('tbody').attr('providername') + ' .visible-offers'; //visibleoffer = .Txy .visible-offers
$(obj).parents('.Provider').find(".Offers:not(.HideByProviderFilter,.HideByMainFilter,.HideByProviderFilter.HideByMainFilter,+ visibleoffer)").length;

So it becomes:

$(obj).parents('.Provider').find(".Offers:not(.HideByProviderFilter,.HideByMainFilter,.HideByProviderFilter.HideByMainFilter,.Txy .visible-offers)").length;

回答1:

You need to concatenate variable while building selector string:

$(obj).parents('.Provider').find(".Offers:not(.HideByProviderFilter,.HideByMainFilter,.HideByProviderFilter.HideByMainFilter," + visibleoffer +")").length;