【问题标题】:Disable button if specific content is present in a class如果类中存在特定内容,则禁用按钮
【发布时间】:2020-04-07 07:34:05
【问题描述】:

我正在开发 Shopify 网站。我已经完成了网站,但我被困在了一个点上。我有一个购物车页面。购物车页面显示了具有我分配给它们的不同标签的不同产品。我想用 jquery 实现的是。如果购物车页面仅显示内容“订阅” 在这个 class="productcarttag" 里面,结帐按钮应该启用它。但是如果它在我的购物车页面中显示内容“订阅”和内容“提货”或“送货”,那么结帐按钮应该会被禁用。

【问题讨论】:

  • 你有模型数据吗?为什么会显示“PICK UP”?是否有显示“PICK UP”的模型数据?如果是这样,您不能将您的启用绑定到模型数据吗?
  • 嗨,所有元素的类相同,但内容不同
  • 是的,我已经为这些产品分配了标签。它是一个 Shopify 网站
  • 你已经在功能上描述了需求,你能不能也把代码放上来让我们明白缺少什么。
  • jQuery('p.productcarttag:contains("Subscription")').css('background-color', 'red');

标签: javascript jquery shopify


【解决方案1】:

您可以使用 vanilla JavaScript 执行以下操作(相同的代码,但使用两个不同的 sn-ps 来显示 2 个不同的要求)。

  1. 仅使用“SUBSCRIPTION”,结帐按钮已启用。

let tag = document.getElementsByClassName("productcarttag")[0];
let button = document.getElementById("button");
if (tag.innerText.includes("SUBSCRIPTION")) {
  button.disabled = false;
  if (
    tag.innerText.includes("PICK UP") ||
    tag.innerText.includes("PICK DELIVERY")
  ) {
    button.disabled = true;
  }
}
<p class="productcarttag">
  "SUBSCRIPTION"
</p>
<button id="button">Checkout</button>
  1. 使用“SUBSCRIPTION”和“PICK UP”或“PICK DELIVERY”,按钮被禁用。

let tag = document.getElementsByClassName("productcarttag")[0];
let button = document.getElementById("button");
if (tag.innerText.includes("SUBSCRIPTION")) {
  button.disabled = false;
  if (
    tag.innerText.includes("PICK UP") ||
    tag.innerText.includes("PICK DELIVERY")
  ) {
    button.disabled = true;
  }
}
<p class="productcarttag">
  "SUBSCRIPTION" and the content "PICK UP" or "DELIVERY"
</p>
<button id="button">Checkout</button>

【讨论】:

  • 如果您单击“运行代码 sn-p”,您会看到它可以工作,您可能需要为您的 shopify 网站调整一些代码。这只是为了给您指明正确的方向,因为您没有分享任何代码,这是我们可以为您提供的最好的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-27
相关资源
最近更新 更多