简体   繁体   English

Hide div包含特定文本,然后隐藏另一个div选项

[英]Hide div contains a certain text then hide another div option

I'm trying to figure out how I can hide a shipping option so that customers won't be able to see and select if one of the courier options includes the wording "Courier Rural" 我试图弄清楚如何隐藏运输选项,以便客户看不到并选择其中一个快递选项是否包含“ Courier Rural”字样。

Summary. 摘要。

Hide Courier DIV (including the shippingprice) option IF Courier Rural DIV exists. 如果存在Courier Rural DIV,则可以使用Hide Courier DIV (包括运费)选项。

Example

<div class="shippingmethodlist">
     <div class="shippingmethod" id="trShippingMethod_1">
        <span id="tdShippingTitle_1" class="shippingmethodname">
          <label class="shippingmethod-label" >Courier</label>
        </span>
        <span class="shippingprice">
          <span id="tdShippingPrice_1">$0.00</span>
        </span>
     </div>
     <div class="shippingmethod" id="trShippingMethod_2">
        <span id="tdShippingTitle_2" class="shippingmethodname">
          <label class="shippingmethod-label" >Pick up in store</label>
        </span>
        <span class="shippingprice">
          <span id="tdShippingPrice_1">$0.00</span>
        </span>
     </div>
     <div class="shippingmethod" id="trShippingMethod_3">
        <span id="tdShippingTitle_3" class="shippingmethodname">
          <label class="shippingmethod-label" >Courier Rural</label>
        </span
        <span class="shippingprice">
          <span id="tdShippingPrice_1">$0.00</span>
        </span>
     </div>
</div>


jq(function () {
 if (jq(".shippingmethod-label").text() == "Courier Rural") {
     jq(".shippingmethod-label").text() == "Courier").hide();
 }
});

EDIT: Let me just clarify that i want the whole div hidden, not just the label 编辑:让我澄清一下,我希望整个div隐藏,而不仅仅是标签

If there is a "Courier Rural" label, iterate over elements with the .shippingmethod-label class and hide its parent if the text is equal to "Courier" 如果有“ Courier Rural”标签,请使用.shippingmethod-label类遍历元素,如果文本等于“ Courier”,则隐藏其父级

$(document).ready(function() {
  if ($(".shippingmethod-label:contains('Courier Rural')").size()) {
    $(".shippingmethod-label").each(function() {
      if ($(this).html() === "Courier") {
        $(this).parent().parent().hide();
      }
    });
  }
});

JSFiddle JSFiddle

Not sure which div you need to hide but try 不知道您需要隐藏哪个div,然后尝试

if ($('.shippingmethod-label:contains("Courier Rural")')) {
    $('.shippingmethodlist').find('.shippingmethod-label:contains("Courier")').hide();
}

This is a great use case for data attributes. 这是数据属性的绝佳用例。 Add an attribute to each of your divs that looks something like this: 为每个div添加一个类似于以下内容的属性:

<div class="shippingmethod" data-purpose="something"></div>

<div class="shippingmethod" data-purpose="somethingelse"></div>

Then you can use the data attribute like you were the label selector in your JS function. 然后,您可以像使用JS函数中的标签选择器一样使用data属性。

jq(function () {
    jq('.shippingmethod[data-purpose="something"]').hide();
});

EDIT (based on comment by OP) 编辑(基于OP的评论)

If you cannot change the template html, and you MUST look for the element inside of the div, then you can use the .parent() function. 如果您不能更改模板html,并且必须在div内查找元素,则可以使用.parent()函数。

jq(function () {
    var courierOption, hideIt;
    jq('.shippingmethod-label').each(function(index, element) {
        if (element.text() === 'Courier Rural') {
            hideIt = true;
        } else if (element.text() === 'Courier') {
            courierOption = element.parent();
        }
    });

    if (hideIt) {
        courierOption.hide();
    }
});

From what I understand you want to hide a different shipping option (let's say "Courier") if the option "Courier Rural" is present. 据我了解,如果存在“ Courier Rural”选项,您想隐藏其他运输选项(例如“ Courier”)。

This is possible. 这个有可能。 You could do something like this: 您可以执行以下操作:

var hideMe;
var shouldHide = false;

$(".shippingmethod-label").each(function() {
  var el = $(this);
  if (el.text() === "Courier") {
    hideMe = el;
  } else if (el.text() === "Courier Rural") {
    shouldHide = true;
  }
});

if (hideMe && shouldHide) {
  hideMe.hide(); // or hideMe.parent().hide(); based on your needs
}

However, if this is for a real site and not just a thought experiment, this should be handled on your back end. 但是,如果这是针对真实网站的,而不仅仅是思想实验,则应在后端进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM