2016-02-29 37 views
0

如何我可以禁用button.single_add_to_cart_button在其他股利,如果我有价格span.amount 0,00€?隐藏按钮,如果价格是0,00€

看看下面的代码:

<form class="cart" enctype="multipart/form-data" method="post" novalidate="novalidate"> 
    <div id="tm-extra-product-options" class="tm-extra-product-options tm-custom-prices tm-product-id-7045 tm-cart-main" data-product-id="7045" data-cart-id="main"> 
     <div class="tm-totals-form-main" data-product-id="7045"> 
      <input class="cpf-product-price" type="hidden" name="cpf_product_price" value="0"> 
      <div id="tm-epo-totals" class="tm-epo-totals tm-custom-prices-total tm-cart-main" data-variations="[]" data-variations-subscription-period="[]" data-subscription-period="" data-variations-subscription-sign-up-fee="[]" data-subscription-sign-up-fee="0" data-prices-include-tax="" data-tax-display-mode="excl" data-tax-string="" data-tax-rate="22" data-taxable="1" data-force-quantity="0" data-tm-epo-dpd-suffix="" data-tm-epo-dpd-prefix="" data-fields-price-rules="0" data-product-price-rules="[]" data-price="0" data-type="simple" data-is-sold-individually="" data-is-subscription="" data-cart-id="main" data-theme-name=""> 
       <dl class="tm-extra-product-options-totals tm-custom-price-totals"> 
        <dt class="tm-options-totals">Options amount</dt> 
        <dd class="tm-options-totals"> 
         <dt class="tm-final-totals">Prezzo Totale:</dt> 
         <dd class="tm-final-totals"> 
          <span class="amount final">0,00€</span> 
         </dd> 
       </dl> 
      </div> 
     </div> 
     <div class="iva_esc"> 
      <div class="quantity"> 
       <input type="hidden" value="7045" name="add-to-cart"> 
       <button class="single_add_to_cart_button button alt" type="submit" style="display: block;">Vai al pagamento</button> 

其他用户,都sended给我这个代码:

<script> 
$(function() { 
    //check if all items are zero 
    var disable = $(".cart span.amount.final").toArray().every(function(item) { 
    return $(item).text() === "0,00€"; 
    }); 

    //disable button if necessary 
    $(".cart button.single_add_to_cart_button").prop("disabled", disable); 
}); 
</script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

此代码工作完美,但现在的价格,当我需要主动为0不同, 00€

+0

凡你需要隐藏它,客户端或服务器端,从而它根本不会出现在输出中?你应该相应地调整你的标签。 – jeroen

+0

所以阅读跨度的文本,如果它是0,比隐藏按钮...你在挣扎什么? – epascarello

+0

if($(“。amount”)。html()===“0,00€”){$(“。your-button”)。attr(“disabled”,true);} 但是最佳实践 - 在服务器端禁用按钮 – esperant

回答

0
var price = $(span.amount).val().split('').map(Number) 

这将拆分任何不是数字的东西,因此您可以使用以下条件:

if(price >= 0) 
    // do smth 
else 
    // do smth else 
+0

其他解决方案? –

+1

让我先检查一下我的解决方案。你想要多少种解决方案? 1似乎不够,所以也许20-30? 嘿,不客气btw。 –

+0

抱歉,但不工作解决方案:( –

0

使用jQuery是这样的:

var value = $('.amount').text(); 
if(value === '0,00€') { 
    $('.single_add_to_cart_button').hide(); 
} else { 
    $('.single_add_to_cart_button').show(); 
} 
+0

工作,但为活动按钮,当价格不是0,00€? –

+0

编辑答案 – marcramser

0

使用jQuery:

<script> 
    var amount = parseFloat($('.amount').html().replace(",", ".")); 
    if(amount === 0){ 
     $('.single_add_to_cart_button').prop('disabled', true); 
    }else{ 
     $('.single_add_to_cart_button').prop('disabled', false);     
    } 
</script> 

JSFiddle Demo

+0

和活动时,价格不是0€? –

+0

检查更新的代码 – mitkosoft

+0

不起作用,button always active –