0

$(document).ready(function() {
  $("input[type=checkbox]").on("change", function() {
    var tot = 0;
    $("input[type=checkbox]:checked").each(function() {
      tot += +(this.value);
    });
    $("#result").text("Total = " + tot);
    $('#CRCGCA').val("Total = " + tot)
  });
});
<p>Gift Certificate Amount (check one or more)
  <br />
  <span class="wpcf7-form-control-wrap GRCGA"><span class="wpcf7-form-control wpcf7-checkbox wpcf7-validates-as-required Multiple radio-vertical" id="GRCGA"><span class="wpcf7-list-item first"><label><input type="checkbox" name="GRCGA[]" value="$10.00 Gift Card" />&nbsp;<span class="wpcf7-list-item-label">$10.00 Gift Card</span>
  </label>
  </span><span class="wpcf7-list-item"><label><input type="checkbox" name="GRCGA[]" value="$20.00 Gift Card" />&nbsp;<span class="wpcf7-list-item-label">$20.00 Gift Card</span>
  </label>
  </span><span class="wpcf7-list-item"><label><input type="checkbox" name="GRCGA[]" value="$25.00 Gift Card" />&nbsp;<span class="wpcf7-list-item-label">$25.00 Gift Card</span>
  </label>
  </span><span class="wpcf7-list-item"><label><input type="checkbox" name="GRCGA[]" value="$40.00 Gift Card" />&nbsp;<span class="wpcf7-list-item-label">$40.00 Gift Card</span>
  </label>
  </span><span class="wpcf7-list-item"><label><input type="checkbox" name="GRCGA[]" value="$100.00 Gift Card" />&nbsp;<span class="wpcf7-list-item-label">$100.00 Gift Card</span>
  </label>
  </span><span class="wpcf7-list-item"><label><input type="checkbox" name="GRCGA[]" value="$200.00 Gift Card" />&nbsp;<span class="wpcf7-list-item-label">$200.00 Gift Card</span>
  </label>
  </span><span class="wpcf7-list-item"><label><input type="checkbox" name="GRCGA[]" value="$400.00 Gift Card" />&nbsp;<span class="wpcf7-list-item-label">$400.00 Gift Card</span>
  </label>
  </span><span class="wpcf7-list-item last"><label><input type="checkbox" name="GRCGA[]" value="$500.00 Gift Card" />&nbsp;<span class="wpcf7-list-item-label">$500.00 Gift Card</span>
  </label>
  </span>
  </span>
  </span>
</p>
<p>Gift Card Amount: <span class="wpcf7-form-control-wrap CRCGCA"><input type="text" name="CRCGCA" value="" size="8" maxlength="8" class="wpcf7-form-control wpcf7-text" id="CRCGCA" aria-invalid="false" /></span>
</p>

<p id="result"></p>

チェックボックス グループの合計を計算する際に問題が発生しています (このチェック ボックス セットは複数選択可能です (1 つまたは複数を選択できます))。ギフト カードの金額というテキスト ボックスに合計をコピーすることもできます。

HTML:

<p>
    Gift Certificate Amount (check one or more)<br />
    [checkbox* GRCGA id:GRCGA class:Multiple use_label_element class:radio-vertical "$10.00 Gift Card " "$20.00 Gift Card " "$25.00 Gift Card " "$40.00 Gift Card " "$100.00 Gift Card " "$200.00 Gift Card " "$400.00 Gift Card " "$500.00 Gift Card"] 
</p>   
Gift Card Amount: [text CRCGCA 8/8 id:CRCGCA]
<p id="result"></p>

JavaScript

$(document).ready(function() {
    $("input[type=checkbox]").on("change", function() {
        var tot = 0;

        $("input[type=checkbox]:checked").each(function() {
            tot += +(this.value);
        });

        $("#result").text("Total = " + tot);
        $('#CRCGCA').val("Total = " + tot)         
    });
});
4

2 に答える 2

1

チェックボックスには次のような値があります$25.00 Gift Card。などの数値に変更する必要があります25

現在のマークアップを使用することを主張する場合は、次を使用して実際の数を取得できます

var val = this.value.substr(1).split(' ')[0];
tot += +(val);

正規表現が得意な人は、おそらくもっときれいなものを見つけ出すことができます。

于 2014-11-17T23:46:16.707 に答える