我想在函数的代码的一部分中为 jquery 元素设置一个条件。这个想法是 input.other[type="checkbox"] 无法取消选中另一个复选框,但可以很好地执行其他操作。这是小提琴
HTML
<input type="checkbox" name="accs" icost="5000" class="arms" />Arm 1: $5000
<br/>
<input type="checkbox" name="accs" icost="6000" class="arms" />Arm 2: $6000
<br/>
<input type="checkbox" name="accs" icost="7000" class="neck" />Neck 1: $7000
<br/>
<input type="checkbox" name="accs" icost="8000" class="neck" />Neck 2: $8000
<br/>
<input type="checkbox" name="accs" icost="12000" class="other" />Other 1: $12000
<br />
<input type="checkbox" name="accs" icost="8000" class="other" />Other 2: $20000
<br />
<div id="total">$5000</div>
javascript代码:
var items = $('input.arms[type="checkbox"], input.neck[type="checkbox"], input.other[type="checkbox"]');
// get the initial total and store in a data for use later (resets as needed)
var total = $('#total');
total.data("currentvalue", parseFloat(total.text().replace(/[^0-9\,]+/g, "")));
items.change(function () {
var currenttotal = total.data("currentvalue");
//exlude input.other[type="checkbox"] from here
var myClass = $(this).attr('class');
var thisGroup = $('.' + myClass);
var notme = $('.' + myClass + ':checked').not(this);
var notmeCost = (notme.length ? notme.attr('icost') : ($(this).is(':checked') ? 0 : $(this).attr('icost')));
notme.prop('checked', false);
//ends here
//include input.other[type="checkbox"] from here
currenttotal = currenttotal - notmeCost;
total.fadeOut(300);
thisGroup.filter(':checked').each(function (i) {
var cost = $(this).attr('icost');
currenttotal = currenttotal + parseFloat(cost);
});
total.data("currentvalue", currenttotal);
total.text("$" + currenttotal.formatMoney(0, ',', '.')); // fix your format here as needed
total.fadeIn(300);
});