簡體   English   中英

如何使用jQuery / JavaScript查找單選按鈕值的總和?

[英]How to find a total sum of radio button values using jQuery/JavaScript?

我找到了以下教程並將其應用於我的代碼如何使用Javascript或jQuery求和單選按鈕值? 但這對我沒有用。 用戶可以選擇計划,並且可以根據自己的意願下載個性化徽標,價格為49.99美元。 我希望徽標的價格為$ 49.99(如果是)或$ 0.00(如果不是),以增加對計划的選擇,並輸出總價。 但是,我以前發布的鏈接中的代碼已起作用,但是我的代碼在做什么錯呢?

小提琴http://jsfiddle.net/1s4gzbys/4/

<div class="control-group questions">
   <div class="field-control">
     <p>Would you like to download your company logo?</p>
        <div class="input-wrapper">
            <div class="answer"><input class="btn-checkbox-choice" type="radio" name="groupeight" value="$49.99"/><label>Yes</label></div>
<input class="btn-checkbox-choice" type="radio" name="groupeight" value="$0.00" /><label>No</label>
           </div>
       </div>
 </div>
 <span id="checkout-title">Checkout</span>
    <div class="plan-wrapper">
      <label id="silver-plan"><input class="btn-checkbox-plan"  type="radio" name="groupnine" value="$699"  /><label>Silver Plan</label><span id="silver-plan-price">$699</span>   </label>
      <label id="gold-plan"><input class="btn-checkbox-plan" type="radio" name="groupnine" value="$999" /><label>Gold Plan</label><span id="gold-plan-price">$999</span></label>                
    </div>
    <div class="logo-wrapper">
      <span id="personalized-logo">Personalized Logo</span>
        <output type="number" name="price" id="output-choice">
    </div>
    <div class="total-wrapper">
        <div class="wrapper-b">
            <span id="total">Total</span>
                <output type="number" name="price" id="output"></output>
        </div>
    </div>

.js文件

 $(document).ready(function(){

 $('input').iCheck({
   checkboxClass: 'icheckbox_square-blue',
   radioClass: 'iradio_square-blue',
   increaseArea: '20%' // optional
});

  // $('input').on('ifChecked', function(event){});
       function calcprice() {
        var sum = 0;
    if(($(".btn-checkbox-choice").is(':checked')) && ($(".btn-checkbox-  plan").is(':checked'))).each(function(){
        sum += parseInt($(this).val(),10);
     });
      $("output[name=price]").val(sum);
     }
     $().ready(function(){
     $("#output").change(function(){
    calcprice()
    });

    });
 });

您可以執行以下操作:

$(document).ready(function(){
     $("input[type=radio]").change(function(){
         if ($("input[name=groupnine]:checked").val() && $("input[name=groupeight]:checked").val()){
             p1 = $("input[name=groupnine]:checked").val().substring(1)*1;
             p2 = $("input[name=groupeight]:checked").val().substring(1)*1;
             $("#output").val("$"+(p1+p2));
         }
         else{
             //alert('no')
         }
      // alert($(this).val());
     })
});

檢查此演示: http : //jsfiddle.net/1s4gzbys/15/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM