2

関数:

function disable_hidden_flip_questions(option, value){
    var first_q = document.getElementById('hidden_auto_questions');
    var second_q = document.getElementById('hidden_include_auto');
    var inputs_list = [];
    if(option == 'all'){
        inputs_list.push(first_q.getElementsByTagName("input"));
        inputs_list.push(second_q.getElementsByTagName("input"));
    }else if (option == 'first'){
        inputs_list.push(first_q.getElementsByTagName("input"));
    }else if(option == 'second'){
        inputs_list.push(second_q.getElementsByTagName("input"));
    }

    for (var inputs in inputs_list){
        for(var input in inputs_list[inputs]){
            if(inputs_list[inputs].hasOwnProperty(input) && input != 'length'){
                if(!value){
                    inputs_list[inputs][input].removeAttribute('disabled');
                }else{
                    inputs_list[inputs][input].setAttribute("disabled", "disabled");
                }
            }
        }
    }
}

Chrome と Firefox では問題なく動作しますが、IE9 ではコンソールに次の行でエラーが表示されます。

inputs_list[inputs][input].setAttribute("disabled", "disabled")

console.log(inputs_list[inputs][input])Chromeでこれを取得した場合:

(入力は RoR によって生成されているため、nameidが非常に長い)

<input class="radio_buttons optional" id="custom_attributes_trailer_insurance_endorsement_false" name="custom_attributes[trailer_insurance_endorsement]" type="radio" value="false" disabled="disabled"> 

ええ、それは私が望んでいた(そして期待していた)ものです...しかし、IE9のコンソールでは次のようになります:

[object HTMLCollection]

まったく役に立たないものは……。

質問: IE の崇高な見方のどこが間違っているのでしょうか? 私は IE9 が をサポートしていることを知っているので、ループsetAttributeに関係していると思います。for...in

編集:コメントのために、問題はオブジェクトタイプ(HTMLCollectionに保存されているArray)にある可能性があります。その場合、すべてを互換性のあるデータ型にするにはどうすればよいですか?

4

2 に答える 2

0

jqueryでの解決策:

  $("input[name=trailer_use]").click(function(event){
        if(event.target.id == "trailer_use_yes"){
            $("#hidden_auto_questions").removeClass('hide_auto').addClass('show_auto');
            $("input[name=trailer_insured]").removeAttr("disabled");
            show_trailer_warning(null, "remove");
        }else {
            $("#hidden_auto_questions").removeClass('show_auto').addClass('hide_auto');
            $("input[name=trailer_insured]").attr("disabled", "disabled");
            if($("#hidden_include_auto").hasClass("show_auto")){
                $("#hidden_include_auto").removeClass('show_auto').addClass('hide_auto');
                $('input[name="custom_attributes[trailer_insurance_endorsement]"]').attr("disabled", "disabled");
            }
            show_trailer_warning(null, "disclaimer");
        }
  });
于 2013-02-06T17:37:53.420 に答える
0

HTMLCollection は実際には配列ではないため、そのように扱うと一貫性のない結果が得られる可能性があります。すべてのブラウザは getElementsByTagName から HTMLCollection を返しますが、それらをどのように処理できるかについては決定的な違いがある場合があります。最善の方法は、DOM メソッドを使用して HTMLCollection からアクセスすることです。つまり、.item(0) を使用して、コレクション自体ではなく DOM ノードを参照していることを確認します ( https://developer.mozilla.orgを参照)。 /en-US/docs/DOM/HTMLCollection )。また、コレクション リストを操作しようとしないので、配列を使用してデータをプッシュする場合は、HTMLCollection ではなく別のデータであることを確認してください。

于 2013-01-30T22:34:08.220 に答える