使用javascript选择选项未显示选择值

时间:2015-06-10 05:51:54

标签: javascript html-select

这里我有一个简单的3个选择选项,例如选择1,选择2并选择3。

更改选择1选项将重置选择2中的值并选择3,并在更改选择2值时调用另一个javascript函数以重置选择3中的值。

这里的问题是首先选择1工作正常并且在从select2选择值时正确显示选择2值并不总是显示一个值。

如果我从select 2中删除了javascript onchange调用,那么它的工作正常。

以下示例代码。



function L1Change() {
    var obj = document.getElementById("L1");
    objVal = obj.options[obj.options.selectedIndex].value;

    var obj2 = document.getElementById("L2");
    obj2.options.length = 0;
    obj2.options[0] = new option("op33","1");
    obj2.options[0].selected = "true";

    if (objVal == 1) {
        obj2.options[1] = new option("op34","2");
        obj2.options[2] = new option("op35","3");
    }

    if (objVal == 2) {
        obj2.options[1] = new option("op44","2");
        obj2.options[2] = new option("op45","3");
    }
}

<select name="L1" id="I1" onChange="javascript:L1Change()">
    <option selected="selected" value="1">op1</option>
    <option  value="2">op2</option>
    <option  value="3">op3</option>
</select>

<select name="L2" id="I2" onChange="javascript:L2Change()">
    <option selected="selected" value="1">op11</option>
    <option  value="2">op12</option>
    <option  value="3">op13</option>
</select>

    
&#13;
&#13;
&#13;

从上面的代码

如果我从select2中的select2值中删除更改函数正常工作。 任何人都可以告诉我如何解决这个问题。

感谢。

1 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
<html>
<head>
<script>
function configureDropDownLists(ddl1,ddl2) {
        var Customer = new Array('Customer1', 'Customer2', 'Customer3');
        var Script = new Array('Script1', 'Script2', 'Script3');
        var etc = new Array('etc1', 'etc2', 'etc3');
        var cust1 = new Array('cust11', 'cust12', 'cust13');
        var cust2 = new Array('cust21', 'cust22', 'cust23');
        var cust3 = new Array('cust31', 'cust32', 'cust33');

        switch (ddl1.value) {
            case 'Customer':
                ddl2.options.length = 0;
                for (i = 0; i < Customer.length; i++) {
                    createOption(ddl2, Customer[i], Customer[i]);
                }
                break;
            case 'Script':
                ddl2.options.length = 0; 
            for (i = 0; i < Script.length; i++) {
                createOption(ddl2, Script[i], Script[i]);
                }
                break;
            case 'etc':
                ddl2.options.length = 0;
                for (i = 0; i < etc.length; i++) {
                    createOption(ddl2, etc[i], etc[i]);
                }
                break;
            case 'Customer1':
                ddl2.options.length = 0;
                for (i = 0; i < etc.length; i++) {
                    createOption(ddl2, cust1[i], cust1[i]);
                }
                break;
            
            case 'Customer2':
                ddl2.options.length = 0;
                for (i = 0; i < etc.length; i++) {
                    createOption(ddl2, cust2[i], cust2[i]);
                }
                break;

            case 'Customer3':
                ddl2.options.length = 0;
                for (i = 0; i < etc.length; i++) {
                    createOption(ddl2, cust3[i], cust3[i]);
                }
                break;

                default:
                    ddl2.options.length = 0;
                break;
        }

    }

    function createOption(ddl, text, value) {
        var opt = document.createElement('option');
        opt.value = value;
        opt.text = text;
        ddl.options.add(opt);
    }
</script>
</head>
<body>

<select id="ddl" onchange="configureDropDownLists(this,document.getElementById('ddl2'))">
<option value=""></option>
<option value="Customer">Customer</option>
<option value="Script">Script</option>
<option value="etc">etc</option>
</select>

<select id="ddl2" onchange="configureDropDownLists(this,document.getElementById('ddl3'))">
</select>

<select id="ddl3">
</select>
</body>
</html>