【问题标题】:How to add back element after i remove it? using .removeAttr in Jquery删除后如何添加回元素?在 Jquery 中使用 .removeAttr
【发布时间】:2022-01-27 01:03:41
【问题描述】:

您好,我有一个例子,如果选择下拉列表 - 可回收物品的价格会出现一个隐藏字段,但我如何在选择服务充电器时隐藏字段消失

我曾尝试使用 .attr 添加回来,但它似乎不起作用

$("#select-price-mode").change(function() {
  if (this.value === "Price By Recyclables") {
    $('.col').removeAttr('hidden');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="form-row">
  <div class="col">
    <label for="select-price-mode" class="col-form-label">Price Mode</label>
    <select class="select-price-mode custom-select-sm col-10" id="select-price-mode" required>
      <option selected disabled value="">Select ....</option>
      <option value="Price By Recyclables">Price By Recyclables</option>
      <option value="Service Charger">Service Charger</option>
    </select>

  </div>

  <div class="col" hidden>
    <label for="select-payment-frequency" class="col-form-label">Payment Frequency</label>
    <select class="select-payment-frequency custom-select-sm col-10" id="select-payment-frequency" required>
      <option selected disabled value="">Select ....</option>
    </select>

  </div>

【问题讨论】:

    标签: html jquery select


    【解决方案1】:

    我认为这将解决您的问题

    $("#select-price-mode").change(function() {
      if (this.value === "Price By Recyclables") {
        $('.col').removeAttr('hidden');
      } else {
        $('.col').attr('hidden', '');
      }
    });
    

    【讨论】:

    • 谢谢哦,我想弄清楚,但为什么我们需要把 , '' 放在后面
    • 因为要设置属性,您需要同时传递 attr 名称和值。如果您在不传递第二个值的情况下执行此操作,则 attr 函数的行为将类似于 getter 并将返回属性的值。你可以在这里阅读更多关于它的信息 - api.jquery.com/attr
    猜你喜欢
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多