检查数据属性和应用逻辑失败

时间:2019-01-07 19:30:18

标签: javascript jquery html css if-statement

在每个div-smallCatalogBlock中,我有一个名为data-availability的数据,其中包含“是”或“否”。我正在尝试检查数据属性的值。如果不是,那么我想要隐藏该div的catalogSmallCircle

到目前为止,什么都没有发生。

有人看到我在做什么错吗?根据我的尝试,应该隐藏“紧固技术”的按钮。

var availability = $('.smallCatalogBlock').data('availability');
$('.smallCatalogBlock').each(function(index, catalogBlock){
		catalogName = $(catalogBlock).data('fill-specs');
		console.log('catalog name ' + catalogName);
		if (availability == 'No') {
			console.log(availability);
			$('.catalogSmallCircle').hide();
		}
		//Filling Circle
		$('.catalogSmallCircle', catalogBlock).html(
			'<div class="catalogSmallCircleIn" data-catalog-name='+ catalogName +'><div class="total-center"><div class="circlePlus"></div></div></div><div class="catalogCircleExpand"><div class="catalogExpandClose"></div><div class="total-center expandText"><span class="catalogName pdfSubHeader"></span><p class="dGw circleExpandText"></p><button class="catalogDownload downloadButton" name="Profile_Catalog" data-catalog-now="Profile Small Catalog Button" data-catalog-view-name="Profile Catalog">View</button><button class="catalogDownload requestButton" data-catalog-name="'+ catalogName +'">Request</button></div></div>'
		)
	});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="smallCatalogWrap">
  <div class="smallCatalogBlock" data-fill-specs="Profile-Catalog" data-availability="Yes">
    <span class="smallCatalogTitle">Profiles and Caps</span>
    <div class="smallCatalogButtonWrap">
      <div class="catalogSmallCircle"></div>
    </div>
  </div><div class="smallCatalogBlock comingSoonSmall" data-fill-specs="Fastening Technology" data-availability="No">
    <span class="smallCatalogTitle">Fastening Technology</span>
    <div class="smallCatalogButtonWrap">
      <div class="catalogSmallCircle"></div>
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:1)

更改

    if (availability == 'No') {
        console.log(availability);
        $('.catalogSmallCircle').hide();
    }

if ($(this).data('availability') === 'no') {
        console.log(availability);
         $(this).find('.catalogSmallCircle').hide();
}

答案 1 :(得分:0)

它看起来像您的可用性变量ist在每个循环之外。相反,应该像fill-specs data属性一样处理它。