0

removeAttr 関数を使用して無効な属性を削除することにより、ドロップダウンのロックを解除する次のコードがあります。この例は、Mozilla Firefox 24 for Ubuntu では機能しません。ただし、次の例に示すように、RemoveAttr 関数の後にアラートを追加すると、問題なく動作します。

        $("#dropdown1").change(function() {
        $('#dropdown2').find('option').remove().end();
        if (obj[$(this).val()] !== undefined)
        {

            $('#dropdown2').removeAttr('disabled');
            $('#dropdown2').append('<option></option>' + obj[$(this).val()]);
            $('#dropdown2').attr('required', true);
        }
        else
        {
            $('#dropdown2').attr('disabled', true);
            $('#dropdown2').attr('required', false);
        }

    });

作業例:

        $("#dropdown1").change(function() {
        $('#dropdown2').find('option').remove().end();
        if (obj[$(this).val()] !== undefined)
        {

            $('#dropdown2').removeAttr('disabled');
            alert("REMOVED");
            $('#dropdown2').append('<option></option>' + obj[$(this).val()]);
            $('#dropdown2').attr('required', true);
        }
        else
        {
            $('#dropdown2').attr('disabled', true);
            $('#dropdown2').attr('required', false);
        }

    });

.prop を使用した例も同様に機能しません:

        $("#dropdown1").change(function() {
        $('#dropdown2').find('option').remove().end();
        if (obj[$(this).val()] !== undefined)
        {
            $('#dropdown2').prop('disabled', false);
            $('#dropdown2').append('<option></option>' + obj[$(this).val()]);
            $('#dropdown2').attr('required', true);

        }
        else
        {
            $('#dropdown2').prop('disabled', true);
            $('#dropdown2').attr('required', false);
        }

    });
4

3 に答える 3

4

.prop()を使用して、無効なプロパティのステータスを設定する必要があります

有効にする

$('#dropdown2').prop('disabled', false);

無効にする

$('#dropdown2').prop('disabled', true);

読む:属性とプロパティ

于 2013-09-26T13:48:15.930 に答える