如何用if语句减去

时间:2015-01-12 01:41:27

标签: javascript jquery wordpress if-statement contact-form-7



   

 <script>
    $(document).ready(function() {
        var inputs = $('input[name="BPSUBPT"], input[name="BPSUPQ"]');
        $(inputs).click(function() {
            var total = 0;
            $(inputs).filter(':checked').each(function() {
                // Now including the - sign
                var value = ($(this).val()).match(/$(-?[0-9]*)/)[1];
                if (value) {
                    // I'm now ADDing the total
                    // total = total + parseInt(value);
                    total += parseInt(value);
                }
            });
            $('#total').html('$' + total);
              $('#BPSUBA').val('$' + total);
    });
        $('input[name="BPSUBPT"]').click(function() {
            $(this).blur();
            $('#BPSUBPP').val($(this).val());
        })
        $('input[name="BPSUPQ"]').click(function() {
            $(this).blur();
            $('#BPSUDA').val($(this).val());
       });
    }); </script>
&#13;
<p>Baby Plan<br />
<span class="wpcf7-form-control-wrap BPSUBPT"><span class="wpcf7-form-control wpcf7-radio radio-vertical" id="BPSUBPT"><span class="wpcf7-list-item first"><input type="radio" name="BPSUBPT" value="Baby Plan $300.00 3 Sessions" />&nbsp;<span class="wpcf7-list-item-label">Baby Plan $300.00 3 Sessions</span></span><span class="wpcf7-list-item last"><input type="radio" name="BPSUBPT" value="Baby Plan $500.00 4 Sessions" />&nbsp;<span class="wpcf7-list-item-label">Baby Plan $500.00 4 Sessions</span></span></span></span> </p>
<p>Did you have a Newborn session With ADP? <br />
<span class="wpcf7-form-control-wrap BPSUPQ"><span class="wpcf7-form-control wpcf7-radio radio-vertical" id="BPSUPQ"><span class="wpcf7-list-item first"><input type="radio" name="BPSUPQ" value="Yes" />&nbsp;<span class="wpcf7-list-item-label">Yes</span></span><span class="wpcf7-list-item last"><input type="radio" name="BPSUPQ" value="No" />&nbsp;<span class="wpcf7-list-item-label">No</span></span></span></span></p>
<p>Baby Plan Totals: <br />
Baby Plan Price: <span class="wpcf7-form-control-wrap BPSUBPP"><input type="text" name="BPSUBPP" value="" size="28" maxlength="28" class="wpcf7-form-control wpcf7-text" id="BPSUBPP" aria-invalid="false" /></span><br />
Discount Amount: <span class="wpcf7-form-control-wrap BPSUDA"><input type="text" name="BPSUDA" value="" size="10" maxlength="10" class="wpcf7-form-control wpcf7-text" id="BPSUDA" aria-invalid="false" /></span><br />
Balance Amount: <span class="wpcf7-form-control-wrap BPSUBA"><input type="text" name="BPSUBA" value="" size="8" maxlength="8" class="wpcf7-form-control wpcf7-text" id="BPSUBA" aria-invalid="false" /></span></p>
<p>Total Price: <span id="total"></span></p>
&#13;
&#13;
&#13;

如何应用if语句并使用JavaScript计算一组单选按钮?

好的我想做的是点击“是”它会减去总额的150美元。

<p>Baby Plan<br />
    [radio BPSUBPT id:BPSUBPT class:radio-vertical "Baby Plan $500.00 3 Sessions" "Baby Plan $700.00 4 Sessions"] </p>

<p>Did you have a Newborn session With ADP? <br />
[radio BPSUPQ id:BPSUPQ class:radio-vertical "Yes" "No"]</p>

<p>Baby Plan Totals: <br />
Baby Plan Price: [text BPSUBPP 28/28 id:BPSUBPP]
Discount Amount: [text BPSUDA 10/10 id:BPSUDA]
Balance Amount: [text BPSUBA 8/8 id:BPSUBA]



Total Price: <span id="total"></span>

我的java脚本

<script>
$(document).ready(function() {
    var inputs = $('input[name="BPSUBPT"], input[name="BPSUPQ"]');
    $(inputs).click(function() {
        var total = 0;
        $(inputs).filter(':checked').each(function() {
            // Now including the - sign
            var value = ($(this).val()).match(/$(-?[0-9]*)/)[1];
            if (value) {
                // I'm now ADDing the total
                // total = total + parseInt(value);
                total += parseInt(value);
            }
        });
        $('#total').html('$' + total);
          $('#BPSUBA').val('$' + total);
});
    $('input[name="BPSUBPT"]').click(function() {
        $(this).blur();
        $('#BPSUBPP').val($(this).val());
    })
    $('input[name="BPSUPQ"]').click(function() {
        $(this).blur();
        $('#BPSUDA').val($(this).val());
   });
}); </script>

1 个答案:

答案 0 :(得分:1)

添加

if($('[name=BPSUPQ]:checked').val() === 'yes')
{
    total = total -150;
}

到这段代码

if (value) 
{
    // I'm now ADDing the total
    // total = total + parseInt(value);
    total += parseInt(value);
}

它将是:

if (value) 
{
    // I'm now ADDing the total
    // total = total + parseInt(value);
    total += parseInt(value);
    if($('[name=BPSUPQ]:checked').val() === 'yes')
    {
        total = total -150;
    }
}

它应该从价格中删除150.

编辑:这应该有效:

&#13;
&#13;
$(document).ready(function() {
  var inputs = $('input[name="BPSUBPT"], input[name="BPSUPQ"]');
  $(inputs).click(function() {
    var total = 0;
    var value = 0;
    $(inputs).filter(':checked').each(function() {
      if ($(this).val() == "Baby Plan $500.00 3 Sessions") {
        value = 500;
      }
      if ($(this).val() == "Baby Plan $700.00 4 Sessions") {
        value = 700;
      }
      if ($(this).val() == 'Yes') {
        total = value - 150;
      } else {
        total = value;
      }
    });
    $('#total').html('$' + total);
    $('#BPSUBA').val('$' + total);
  });
  $('input[name="BPSUBPT"]').click(function() {
    $(this).blur();
    $('#BPSUBPP').val($(this).val());
  });
  $('input[name="BPSUPQ"]').click(function() {
    $(this).blur();
    $('#BPSUDA').val($(this).val());
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Baby Plan
  <br />
  <span class="wpcf7-form-control-wrap BPSUBPT"><span class="wpcf7-form-control wpcf7-radio radio-vertical" id="BPSUBPT"><span class="wpcf7-list-item first"><input type="radio" name="BPSUBPT" value="Baby Plan $500.00 3 Sessions" />&nbsp;<span class="wpcf7-list-item-label">Baby Plan $500.00 3 Sessions</span></span>
  <span
  class="wpcf7-list-item last">
    <input type="radio" name="BPSUBPT" value="Baby Plan $700.00 4 Sessions" />&nbsp;<span class="wpcf7-list-item-label">Baby Plan $700.00 4 Sessions</span></span>
    </span>
    </span>
</p>
<p>Did you have a Newborn session With ADP?
  <br />
  <span class="wpcf7-form-control-wrap BPSUPQ"><span class="wpcf7-form-control wpcf7-radio radio-vertical" id="BPSUPQ"><span class="wpcf7-list-item first"><input type="radio" name="BPSUPQ" value="Yes" />&nbsp;<span class="wpcf7-list-item-label">Yes</span></span>
  <span
  class="wpcf7-list-item last">
    <input type="radio" name="BPSUPQ" value="No" />&nbsp;<span class="wpcf7-list-item-label">No</span></span>
    </span>
    </span>
</p>
<p>Baby Plan Totals:
  <br />Baby Plan Price: <span class="wpcf7-form-control-wrap BPSUBPP"><input type="text" name="BPSUBPP" value="" size="28" maxlength="28" class="wpcf7-form-control wpcf7-text" id="BPSUBPP" aria-invalid="false" /></span>
  <br />Discount Amount: <span class="wpcf7-form-control-wrap BPSUDA"><input type="text" name="BPSUDA" value="" size="10" maxlength="10" class="wpcf7-form-control wpcf7-text" id="BPSUDA" aria-invalid="false" /></span>
</p>
<p><span class="wpcf7-form-control-wrap BPSU"><span class="wpcf7-quiz-label">What day comes after Sunday?</span>&nbsp;
  <input type="text" name="BPSU" size="10" maxlength="12" class="wpcf7-form-control wpcf7-quiz" id="BPSU" aria-required="true" aria-invalid="false"
  />
  <input type="hidden" name="_wpcf7_quiz_answer_BPSU" value="66848d18bdcdad7635a3790efc125e1c" />
  </span>
</p>
<p>Total Price: <span id="total"></span>
</p>
&#13;
&#13;
&#13;