0

Is there a more efficient, DRYer way of writing the if-else-statement in following piece of code? It just doesn't look nice.

var $diffPaymentAddressFieldset = $('fieldset.diffPaymentAddress', 'form.myForm');

// $checkbox is an input with type=checkbox
if ($checkbox.is(':checked') === true) { 
    $diffPaymentAddressFieldset.removeClass('hidden');
} else {
    $diffPaymentAddressFieldset.addClass('hidden');
}

(FYI: Ignore the long variable names, they'll get minified.)

4

2 回答 2

4

Use toggleClass()

$diffPaymentAddressFieldset.toggleClass('hidden', $checkbox.is(':checked'));
于 2012-04-29T13:32:47.387 回答
0

还是带有 addClass/removeClass 的单线?

$diffPaymentAddressFieldset[$checkbox.is(':checked')?'removeClass':'addClass']('hidden');

另外,看在上帝的份上,尝试使用较短的变量名?

于 2012-04-29T13:39:45.293 回答