2

次の js は FF2 で正常に動作しますが、IE6 では、ドロップダウンは常に IE -> testix2 対 FF2 -> testix3 の 1 つのオプションを早期に選択します。スクリプトのどこかに alertBox を追加すると、IE6 でも正常に動作します。しかし、alertBox なしでこれを解決するにはどうすればよいでしょうか?

ティア

<script language="JavaScript" type="text/javascript">
<!--
function Entry(value, name, selected) {
    this.value = value;
    this.name = name;
    this.selected = selected;
}


//-->
</script>
        <select id="selSeaShells">

        </select>
<script language="JavaScript" type="text/javascript">
<!--
var productCategoryLevel2 = new Array();

productCategoryLevel2.push(new Entry('Hallo1', 'testix1', false));
productCategoryLevel2.push(new Entry('Hallo2', 'testix2', false));
productCategoryLevel2.push(new Entry('Hallo3', 'testix3', true));

    var i = 0;
        for (i in productCategoryLevel2) {
        var optL2 = document.createElement('option');
        optL2.selected = true;

                optL2.text = productCategoryLevel2[i].name;
        optL2.value = productCategoryLevel2[i].value;
        if (productCategoryLevel2[i].selected == true) {
            productCategoryLevel2[i].selected = true;
            optL2.selected = true;
        } else {
            optL2.selected = false;     
        }
                try {
            document.getElementById("selSeaShells").add(optL2, null);
                } catch(ex3) {
            document.getElementById("selSeaShells").add(optL2);
                }
        }
//-->
</script>
4

3 に答える 3

3

あなたの例がうまくいかない理由は完全にはわかりませんが、このようにすればうまくいきます(つまり、 <option> の selected プロパティを設定するのではなく、 <select> に selectedIndex を設定します)。FF3、IE6、Chromeでテスト済み。

var i = 0;
for (i in productCategoryLevel2) {
    var optL2 = document.createElement('option');

    optL2.text = productCategoryLevel2[i].name;
    optL2.value = productCategoryLevel2[i].value;
    try {
        document.getElementById("selSeaShells").add(optL2, null);
    } catch(ex3) {
            document.getElementById("selSeaShells").add(optL2);
    }

    if (productCategoryLevel2[i].selected == true) {
            document.getElementById("selSeaShells").selectedIndex = i;
    }      
}
于 2008-12-22T12:18:57.817 に答える
0

現在これをテストすることはできませんが、JavaScriptのオブジェクトでのみ使用する必要があるfor..in構文ではなく、標準のfor(;;)ループを使用して配列を反復処理することをお勧めします。

于 2008-12-22T12:31:59.943 に答える
0

これはIEのバグのように感じますが(trueを1に変換する際の問題かどうか疑問に思います)、tomhaighが修正するselect自体ではなく、オプションに基づいてselectインデックスを作成しました。

ゼロのオプションから始める場合、selectの最初のオプションをどの値に指定するかは重要ではないことを理解することが重要です。要素として、1つのセットの1つとして、selected=trueのみが可能です。

(私も三連祭壇画に同意しforます)

于 2008-12-22T12:44:30.527 に答える