1父母有2个孩子下拉

时间:2008-12-12 04:47:38

标签: javascript drop-down-menu parent-child

我正在尝试使用JAVASCRIPT创建1个父下拉列表,其中包含2个从属子下拉列表。

我的html页面位于 - http://www.larkgrove.com/entryform/entryform.html

我正在使用动态选项列表/从属选择: http://www.javascripttoolbox.com/lib/dynamicoptionlist/examples.php

如果您查看我的网站,您可以看到我可以让Child列表在没有任何内容和“NULL”之间进行更改,但这就是我能做的所有事情。

谢谢!

2 个答案:

答案 0 :(得分:1)

我知道您正在使用动态选项脚本,但我认为我会提供一个快速从头开始的解决方案。代码有点冗长,但我希望通过这种方式更容易看到。最终工作页面位于:http://ryanscook.com/Files/DropDownListTest.htm

让我们假设你有这个html:

<select id="parentList" onchange="parentList_OnChange(this)">
    <option>Choose an option</option>
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    <option value="D">D</option>
</select>

<select id="childList1"></select>
<select id="childList2"></select>

您会注意到我们有一个onchange处理程序,这是java脚本:

// Data for child list 1, this is a of the parent value to one or more options
var childList1Data = {
    "A": ["ChildList1 - A1", "ChildList1 - A2", "ChildList1 - A3"],
    "B": ["ChildList1 - B1"],
    "C": ["ChildList1 - C1", "ChildList1 - C2"],
    "D": ["ChildList1 - D1", "ChildList1 - D2"]
};


// Data for child list 2, this is a of the parent value to one or more options
var childList2Data = {
    "A": ["ChildList2 - A1", "ChildList2 - A2"],
    "B": ["ChildList2 - B1", "ChildList2 - B2", "ChildList2 - B3"],
    "C": ["ChildList2 - C1", "ChildList2 - C2"],
    "D": ["ChildList2 - D1"]
};


// onchange is called when the parent value is changed
function parentList_OnChange(objParentList) {
    var child1 = document.getElementById("childList1");
    var child2 = document.getElementById("childList2");

    // Remove all options from both child lists
    removeOptions(child1);
    removeOptions(child2);

    // Lookup and get the array of values for child list 1, using the parents selected value
    var child1Data = childList1Data[objParentList.options[objParentList.selectedIndex].value];

    // Add the options to child list 1
    if (child1Data) {
        for (var i = 0; i < child1Data.length; i++) {
            child1.options[i] = new Option(child1Data[i], child1Data[i]);
        }
    }


    // Do the same for the second list
    var child2Data = childList2Data[objParentList.options[objParentList.selectedIndex].value];

    if (child2Data) {
        for (var i = 0; i < child2Data.length; i++) {
            child2.options[i] = new Option(child2Data[i], child2Data[i]);
        }
    }
}


function removeOptions(objSelect) {
    while (objSelect.options.length > 0)
        objSelect.options[0] = null;
}

我希望这会有所帮助,并且离你的问题不远。

答案 1 :(得分:0)

嗯,首先,让我们来看看你的代码:

<script type="text/javascript">
var TESTLIST = new DynamicOptionList("PARENT1","CHILD1","CHILD2");
TESTLIST.forValue("A").forValue("A").forValue("A").addOptionsTextValue("C","C","D","D");
</script>
<select name="PARENT1">
    <option value="A">A</option>
    <option value="B">B</option>
</select>
<br /><br />
<select name="CHILD1"><script type="text/javascript">TESTLIST.printOptions("CHILD1")</script></select>
<br /><br />
<select name="CHILD2"><script type="text/javascript">TESTLIST.printOptions("CHILD2")</script></select>

我不认为你正在做你打算做的事情,特别是这个:

TESTLIST.forValue("A").forValue("A").forValue("A").addOptionsTextValue("C","C","D","D");

如何冗余地多次调用.forvalue(“A”)?如果你看一下你看到的代码示例:

regionState.forValue("west").addOptions("California","Washington","Oregon");

我会尝试更像这样的事情:

TESTLIST.forValue("A").addOptionsTextValue("C","D");
TESTLIST.forValue("B").addOptionsTextValue("E","F");
豫ICP备18024241号-1