1 名家长带着 2 名孩子下拉

发布于 2024-07-10 18:29:55 字数 505 浏览 7 评论 0原文

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

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

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

如果您查看我的网站,您会发现我可以让子列表在“无”和“NULL”之间进行更改,但这就是我能做的。

谢谢!

I am trying to create 1 Parent Drop Down, that has 2 dependent child drop down lists using JAVASCRIPT.

My html page is at - http://www.larkgrove.com/entryform/entryform.html

I am using the Dynamic Options Lists / Dependent Selects:
http://www.javascripttoolbox.com/lib/dynamicoptionlist/examples.php

If you check out my site, you can see that I can get the Child lists to change between nothing there and "NULL", but thats about all I can do.

THANKS!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小忆控 2024-07-17 18:29:55

我知道您正在使用动态选项脚本,但我想我会提出一个从头开始的快速解决方案。 代码有点冗长,但我希望这样可以更容易地了解发生了什么。 最终工作页面在这里: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;
}

我希望这会有所帮助,并且与您的问题相距不远。

I know you are using the Dynamic options script, but I thought that I would put up a quick from scratch solution. The code is a little verbose, but I am hoping it will be easier to see whats going on this way. The final working page is here: http://ryanscook.com/Files/DropDownListTest.htm

Lets start by assuming you have this 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>

You will notice that we have an onchange handler, here is the java script:

// 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;
}

I hope this helps and was not to far off from your question.

镜花水月 2024-07-17 18:29:55

好吧,首先,让我们看一下您的代码:

<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");

Well, first of all, let's have a look at your code:

<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>

I don't think that's doing what you are intending it to do, especially this:

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

See how you call .forvalue("A") several times redundantly? If you look at the code example you see:

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

I would try something more like this:

TESTLIST.forValue("A").addOptionsTextValue("C","D");
TESTLIST.forValue("B").addOptionsTextValue("E","F");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文