【问题标题】:Js Convert Object To Number Return NanJs 将对象转换为数字返回 Nan
【发布时间】:2022-12-05 15:58:47
【问题描述】:

对于客户,我想告诉他们,如果他们激活的当前货币的产品价格等于零,则更改他们的货币并且无法添加到购物车,但无论如何,我尝试获取金额内的数据。楠回来了

我的 HTML 代码

<ul>
<li>
<span id="itemprice"><span class="sym-curr">$</span>0.00</span>
</li>
<ul>
<button type="button" id="button-cart" class="product-add-to-basket">
    <span class="add-to-cart-text">add To cart</span>
</button>

我的代码

var priceProduct = document.getElementById('itemprice').lastChild;
$('#button-cart').on('click', function() {
  if(priceProduct == 0) {
   The rest of the code
  } else {
   The rest of the code
  }
}

【问题讨论】:

    标签: javascript html jquery


    【解决方案1】:

    原因是你定义了priceProduct为全局变量,点击前会执行,所以值为NaN

    为了解决它,你只需要每次点击它时得到价格

    $('#button-cart').on('click', function() {
      // make it inside the click function
      var priceProduct = document.getElementById('itemprice').lastChild;
      if(priceProduct == 0) {
       The rest of the code
      } else {
       The rest of the code
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-25
      • 2020-10-24
      • 2011-09-09
      • 2023-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多