JQuery disable form element when checked

I have a complex jQuery form, and I want to disable some form elements if a certain checkbox is selected. I am using jQuery 1.3.2 and all related plugins. What am I doing wrong here? Thanks Dakota

Here is my HTML:

    <li id="form-item-15" class="form-item">
        <div class='element-container'>
            <select id="house_year_built" name="house_year_built" class="form-select" >
                 ...Bunch of options...
            </select>
            <input type="text" title="Original Purchase Price" id="house_purchase_price" name="house_purchase_price" class="form-text money" />
            <input type="text" title="Current Home Debt" id="house_current_debt" name="house_current_debt" class="form-text money" />
        </div>
        <span class="element-toggle">
            <input type="checkbox" id="house_toggle" />
            <span>Do not own house</span>
        </span>
    </li>

Here is my jQuery:

$('.element-toggle input').change(function () { 
    if ($(this).is(':checked')) $(this).parents('div.element-container').children('input,select').attr('disabled', true);
    else $(this).parents('div.element-container').children('input,select').removeAttr('disabled'); }); 
+5
source share
4 answers

, . , HTML , JavaScript ( , parents()). -, click IE, IE , change, .

$('.element-toggle input').bind('click', function () {
    var inputs = $(this).closest('li.form-item').find('div.element-container').children('input,select');

    if ($(this).attr('checked')) {
        inputs.attr('disabled', true);
    } else {
        inputs.removeAttr('disabled');
    }
});
+4

: jquery 1.6 $(this).attr('checked'), , $(this).prop('checked').

$(this).prop('disabled') ( , ) $(this).prop('disabled', newState), attr.

+10

<div class='element-container'> $('.element-toggle input') -

0

All Articles