[英]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.