【问题标题】:Jquery Hide Div element if length greater then如果长度大于 Jquery 隐藏 Div 元素
【发布时间】:2021-08-19 09:04:12
【问题描述】:

如果另一个div的长度大于0,我想隐藏元素,否则显示div

我有这个代码,但它不适合我:(

 <div id="myid">90.00</div>
 <div class="myclass">99.00</div>

JS代码

$(document).ready(function() {
           if($('#myid').length > 2){
            $('.myclass').hide();
        }
    });

【问题讨论】:

  • 另一个 div 的长度大于两个符号是什么意思?
  • 对不起,我的意思是如果 div id 大于 0 隐藏类
  • 你的意思是要有身份证吗?!! OR id 的值长度大于一个数字?!
  • Length 函数将返回 ID 为“mydiv”的分区总数,而不是该分区的长度。请改写除法长度的确切含义
  • 请检查下面的代码

标签: javascript jquery logic


【解决方案1】:

首先用你的新细节编辑你的问题;

然后在 updateTotalFinal 函数中设置条件

jQuery(document).ready(function() {
  function updateTotalFinal(){
    const subtotalValue = +jQuery('.wpforms-payment-total').text().replace(/^[^0-9,.]+/, "").match( /^[0-9,.]+/g, '');
    let totalValue = subtotalValue;
    const totalFinalElem = jQuery("#totalfinal");
    
  
    
    jQuery("input.change-total:checked").each(function() {
      const $this = jQuery(this);
      if ($this.is("[data-method=add]")) {
        totalValue = totalValue * $this.data("amount");
      } else if ($this.is("[data-method=multiply]")) {
        totalValue += subtotalValue * $this.data("amount");
      }
    });
    
    totalFinalElem.text(`${totalValue.toFixed(2)}`);
    
    // IF YOU WANT CHECK THAT ANY VALUE FOR totalfinal IS EXIST USE THIS
   if($('#totalfinal').text().length ){
      $('.wpforms-payment-total').hide();
    }
    
    // IF YOU WANT CHECK THAT totalfinal IS GREATHER THAN ZERO USE THIS
    /* if(parseInt( $('#totalfinal').text()) > 0 ){
      $('.wpforms-payment-total').hide();
    } */
  }
  
  jQuery("input.change-total").on("change", function() {
    updateTotalFinal();
   
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wpforms-payment-total">
     97
    </div>
     
    
    <tr>
    <td>
      <input type="checkbox" class="change-total"  name="myBox2" size="12" data-amount="0.15" data-method="multiply" value="100" />
      With Two Coat + 15%
    </td>
    
    <td>
      <input type="checkbox" class="change-total" name="myBox3" size="12" id="myCheck" data-amount="0.9" data-method="add" />
      With My Own Paint
      </td>
    </tr>
    <br>
    <b><span id="totalfinal"></span></b>

【讨论】:

  • 谢谢你,很抱歉这个愚蠢的问题。我可以再问一件事,我只想在小数点后显示 2 个符号。这行得通吗? ` if($('#totalfinal').text(finalprice.toFixed(2)).length ){` ?
  • 欢迎您。如果finalprice 表示totalValue 你必须在这里使用toFixed(2) totalFinalElem.text(${totalValue.toFixed(2)})$('#totalfinal').text().length 再次工作
【解决方案2】:

我有这段代码,想隐藏 计算“totalfinal”时的“wpforms-payment-total”类

<div class="wpforms-payment-total">
     97
    </div>
     
    
    <tr>
    <td>
      <input type="checkbox" class="change-total"  name="myBox2" size="12" data-amount="0.15" data-method="multiply" value="100" />
      With Two Coat + 15%
    </td>
    
    <td>
      <input type="checkbox" class="change-total" name="myBox3" size="12" id="myCheck" data-amount="0.9" data-method="add" />
      With My Own Paint
      </td>
    </tr>
    <br>
    <b><span id="totalfinal"></span></b>

JS

jQuery(document).ready(function() {
  function updateTotalFinal(){
    const subtotalValue = +jQuery('.wpforms-payment-total').text().replace(/^[^0-9,.]+/, "").match( /^[0-9,.]+/g, '');
    let totalValue = subtotalValue;
    const totalFinalElem = jQuery("#totalfinal");
    
  
    
    jQuery("input.change-total:checked").each(function() {
      const $this = jQuery(this);
      if ($this.is("[data-method=add]")) {
        totalValue = totalValue * $this.data("amount");
      } else if ($this.is("[data-method=multiply]")) {
        totalValue += subtotalValue * $this.data("amount");
      }
    });
    
    totalFinalElem.text(`$ ${totalValue}`);
  }
  
  jQuery("input.change-total").on("change", function() {
    updateTotalFinal();
  });
});



$(document).ready(function() {
  if($('#totalfinal').text() > 0){
      $('.myclass').hide();
  }
});

【讨论】:

    猜你喜欢
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多