【发布时间】:2014-12-18 04:36:15
【问题描述】:
我查看了以下教程:
How might I calculate the sum of radio button values using jQuery?
How to find a total sum of radio button values using jQuery/JavaScript?
How might I calculate the sum of radio button values using jQuery?
但是,这些教程都没有帮助我编写代码。
我有大约 100 个单选按钮来询问各种问题。其中一个单选按钮询问用户是否要下载徽标;如果是,则价格为 49.99 美元,否则为 0.00 美元。用户必须选择 Silver 或 Gold 计划。所选计划的价格必须添加到徽标价格中,并显示总价。
这是我目前所拥有的:
$(document).ready(function(){
$('input').iCheck ({
checkboxClass: 'icheckbox_square-blue',
radioClass: 'iradio_square-blue',
increaseArea: '20%' // optional
});
var sum = 0;
$('input').on("change", function(){
sum = parseInt($('input[name="groupeight"]:checked').val(),10) + parseInt($('input[name="groupnine"]:checked').val(),10);
console.log(sum);
if($('input[name="groupeight"]').is(':checked') && $('input[name="groupnine"]').is(':checked')){
$('#output').html("$"+sum);
}
});
$(".choice").blur(function(){
$(this).parseNumber({format:"#,###.00", locale:"us"});
$(this).formatNumber({format:"#,###.00", locale:"us"});
});
$(".plan").blur(function(){
$(this).parseNumber({format:"#,###.00", locale:"us"});
$(this).formatNumber({format:"#,###.00",locale:"us"});
});
});
<div class="input-wrapper">
<div class="answer"><input class="choice" type="radio" name="groupeight" value="49.99"/>
<label>Yes</label>
</div>
<input class="choice" type="radio" name="groupeight" value="0.00" /><label>No</label><br />
</div>
<div class="plan-wrapper">
<label id="silver-plan"><input class="plan" type="radio" name="groupnine" value="699" <?php if (!isset($_POST['radio']) || isset($_POST['radio']) && $_POST['radio'] == '699'): ?>checked="checked"<?php endif; ?> /><label>Silver Plan</label><span id="silver-plan-price">$699</span></label>
<label id="gold-plan"><input class="plan" type="radio" name="groupnine" value="999" <?php if (isset($_POST['radio']) && $_POST['radio'] == '999'): ?>checked="checked"<?php endif; ?>/><label>Gold Plan</label><span id="gold-plan-price">$999</span></label>
</div>
【问题讨论】:
标签: javascript jquery html