5

複雑な jQuery フォームがあり、特定のチェックボックスがオンになっている場合、いくつかのフォーム要素を無効にしたいと考えています。jQuery 1.3.2 と関連するすべてのプラグインを使用しています。ここで何が間違っていますか?

ここに私の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>

これが私の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'); }); 
4

4 に答える 4

10

ちょっとしたこと: jquery 1.6 以降、 which$(this).attr('checked')は常に true ではなく、 を使用する必要があります$(this).prop('checked')

$(this).prop('disabled')また、(要素が無効になっているかどうかをテストするために)を使用することをお勧めし$(this).prop('disabled', newState)ますattr

于 2011-12-16T22:50:53.413 に答える
4

変更すべき点がいくつかあります。まず第一に、実際の HTML 構造は、JavaScript で照会しているもの (具体的にはparents()呼び出し) と一致しません。第 2 に、IEは要素がフォーカスを失うまでイベントの発生を待機する場合があるため、イベントへのバインディングclickは 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');
    }
});
于 2009-11-25T19:16:14.897 に答える
1

それは役に立ちました。プラグインをお探しの方は、http ://s.technabled.com/jquery-foggleをご覧ください。

于 2011-01-30T23:53:45.557 に答える
0

<div class='element-container'>$('.element-toggle input')あなたの例の親ではありません

于 2009-11-25T19:10:10.233 に答える