【问题标题】:How to substract with if statement如何用if语句做减法
【发布时间】:2015-01-12 01:41:27
【问题描述】:

   

 <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>
<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>

如何应用 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>

【问题讨论】:

  • 请发布正确的 html,我们不是 wordpress 短代码处理器,语法荧光笔也不能很好地处理它
  • 添加了代码片段花了我一段时间才弄明白,但我现在搞定了
  • 任何帮助,我需要这个尽快!请!
  • 类似if( $('[name=BPSUPQ]:checked').val() === 'yes'){ total = total -150;}
  • 感谢您的回答,但请您告诉我该去哪里

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


【解决方案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。

编辑:这应该工作:

$(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());
  });
});
<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>

【讨论】:

  • 因为存在 javascript 错误或计算不正确?
  • 我不知道我是否可以再次尝试添加一个片段
  • 没关系一对多 });最后它现在正在工作
猜你喜欢
  • 2016-02-28
  • 1970-01-01
  • 2022-12-16
  • 1970-01-01
  • 2023-01-13
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 1970-01-01
相关资源
最近更新 更多