简体   繁体   English

下拉值我不想使用javascript显示

[英]Dropdown values I do not want to display using javascript

I have two dropdown menus. 我有两个下拉菜单。

1st -> Drop Down Menu (name="DropDown1") has 2 values which are ABC and ABCD 1st - > Drop Down Menu(name =“DropDown1”)有2个值,分别是ABC和ABCD
2nd -> Drop Down Menu (name="DropDown2") has 3 values which are 1, 2 and 3. 2nd - >下拉菜单(name =“DropDown2”)有3个值,分别为1,2和3。

Now my question is that in Javascript if the user selects ABC from DropDown1, then how can I get it so that DropDown2 only shows values 1 and 2 only and not 3, this is because you can't pick 3 answers for ABC, but then be able to show values 1, 2 and 3 in DropDown2 if user selects ABCD from DropDown1. 现在我的问题是,在Javascript中,如果用户从DropDown1中选择ABC,那么我怎样才能得到它以便DropDown2只显示值1和2而不是3,这是因为你不能为ABC选择3个答案,但是如果用户从DropDown1中选择ABCD,则能够在DropDown2中显示值1,2和3。

Thank You 谢谢

I would set up an object of key/value pairs that hold your selection possibilities. 我会设置一个键/值对的对象,以保持您的选择可能性。 Then bind to the change event on DropDown1 so that you rewrite the options of DropDown2 when it changes. 然后绑定到DropDown1上的change事件,以便在更改时重写DropDown2的选项。

Your javascript object would look something like this... 你的javascript对象看起来像这样......

var DropOne = new Array();

DropOne.ABC = [1, 2];
DropOne.ABCD = [1, 2, 3];

If you add the jQuery tag to the question I'd happily show you some working code. 如果你将jQuery标签添加到问题中,我很乐意向你展示一些有用的代码。 I couldn't do it in pure javascript off the top of my head though... whoops :) 我不能在纯粹的javascript中做到这一点虽然...哎呀:)

Alright... well here it is with jQuery if you want it... 好吧......好吧,如果你想要它,它就是jQuery ...

http://jsfiddle.net/brettzink/pND9u/ http://jsfiddle.net/brettzink/pND9u/

You have to have two Arrays and on selection of question you have to populate options in Answer select, Sample Code: 您必须有两个数组,并且在选择问题时,您必须填写答案选择,示例代码中的选项:

<html>
    <head>
    <title>Question Answer Test</title>
    <script type="text/javascript">
        var dropdown1 = [];
        var dropdown2 = [];

        dropdown1[0] = new Option("1", "1");
        dropdown1[1] = new Option("2", "2");

        dropdown2[0] = new Option("1", "1");
        dropdown2[1] = new Option("2", "2");
        dropdown2[2] = new Option("3", "3");

        function populateAnswer(qusSelectBox, ansSelectBox){
            var tempMenuItem;
            ansSelectBox.options.length = 0;

            switch (qusSelectBox.selectedIndex) {
                case 0:
                    tempMenuItem = dropdown1;
                    break;
                case 1:
                    tempMenuItem = dropdown2;
                    break;
           }

           for (var i = 0; i < tempMenuItem.length; i++) {
                ansSelectBox.options[i] = tempMenuItem[i];
           }
   }

    </script>
    </head>
    <body>
        <form name="Menus">
            <select name="Question" onchange="populateAnswer(this, this.form.Answer);">
                 <option value="ABC">ABC</option>
                 <option value="ABCD">ABCD</option>
            </select> 
            <select name="Answer">
                <option value="1">1</option>
                <option value="2">2</option>
            </select>
        </form>
    </body>
 </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在html下拉列表中显示JSON数据键(不是值) - I want to display JSON data keys in html dropdown (not values) 根据javascript中的值显示下拉列表 - Display dropdown based on values in javascript 我想使用一个下拉菜单,它将使用JavaScript更改文档的bgColor - I want to use a dropdown menu that will change the bgColor of the document using JavaScript 如何在下拉字段中显示JavaScript值? - How do I display a javascript value in dropdown field? 如何在JavaScript中使用this.value从下拉列表中检查特定值 - how do i check for specific values from a dropdown list using this.value in javascript 如何使用下拉列表中的值在 javascript 中创建函数? - How do I create functions in javascript using values from a dropdown list? 我有一个字符数组,我想使用 javascript 在 html 中以 4 行的形式显示名称、房屋和图像。 我是foreach吗? - I have a array of characters, I want to display the name, house and image in rows of 4 in html using javascript. Do I foreach? 如何使用下拉菜单触发javascript事件并更改值? - how do i trigger the javascript event with dropdown and change the values? 我想使用javascript运行脚本,该怎么办? - I want to run a script using javascript how do I do it? 我想使用 Javascript 在可编辑控件中显示 {00.00} 这种格式吗? - I want to display {00.00} this format in editable control with using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM