I am trying to create a drop-down select menu that will consist of three select boxes, where the 3rd box will show/hide specific option based on an option selected in the 1st select box.
我正在尝试创建一个下拉选择菜单,其中包含三个选择框,其中第三个框将根据在第一个选择框中选择的选项显示/隐藏特定选项。
I was wondering if anyone here is be able to suggest a solution to this.
我想知道这里是否有人能够提出解决方案。
Here is a simplified version of the 3 select boxes I have:
这是我所拥有的3个选择框的简化版本:
<select class="product" id="select_1" name="product">
<option selected="selected" value=""> Choose Category </option>
<option value="Mens Suits"> Mens Suits </option>
<option value="Womens Suit"> Womens Suits </option>
<option value="Children Suit"> Children Suits </option>
</select>
<select class="color" id="select_2" name="color">
<option selected="selected" value=""> Choose Color </option>
<option value="Blue">Blue</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
</select>
<select class="size" id="select_3" name="size">
<option selected="selected" value=""> Choose Size </option>
<!-- Mens Sizes Below -->
<option value="36">36</option>
<option value="38">38</option>
<option value="40">40</option>
<!-- Womens Sizes Below -->
<option value="30">30</option>
<option value="29">29</option>
<option value="28">28</option>
<!-- Children Sizes Below -->
<option value="12">12</option>
<option value="11">11</option>
<option value="10">10</option>
</select>
With the example above, I would like to be able to view the first 3 options from the 3rd select box (36
, 38
, and 40
) when the option Mens Suits
from the 1st select box is chosen. Similarly, when the Womens Suits
is selectedfrom the 1st box, the options 30
, 29
, and 28
should be visible in the 3rd box. The same with the Children Suits.
通过上面的示例,我希望能够在第一个选择框中选择Mens Suits选项时从第三个选择框(36,38和40)查看前3个选项。类似地,当从第一个框中选择女性套装时,选项30,29和28应该在第3个框中可见。与儿童套装相同。
I hope this makes sense. Thank you.
我希望这是有道理的。谢谢。
7 个解决方案
#1
5
Try:
尝试:
var men = '<option selected="selected" value=""> Choose Size </option><option value="36">36</option><option value="38">38</option><option value="40">40</option>';
var women = '<option selected="selected" value=""> Choose Size </option><option value="30">30</option><option value="29">29</option><option value="28">28</option>';
var children = '<option selected="selected" value=""> Choose Size </option><option value="12">12</option><option value="11">11</option><option value="10">10</option>';
$(document).ready(function(){
$("select#select_1").on('change',function(){
if($(this).val()=="Mens Suits"){
$("select#select_3").html(men);
}else if($(this).val()=="Womens Suit"){
$("select#select_3").html(women);
}else if($(this).val()=="Children Suit"){
$("select#select_3").html(children);
}
});
});
DEMO FIDDLE
#2
2
I tried in many different ways but this solution seems reasonable and I have used in my code. No plugins required simple register function with jquery object
我试过很多不同的方法,但这个解决方案似乎很合理,我在我的代码中使用过。没有插件需要使用jquery对象的简单注册函数
Solution at glance:
解决方案一览:
(function ($) {
$('#showOne').click(function () {
$('#ddlNumbers').showHideDropdownOptions('3', true);
});
$('#hideOne').click(function () {
$('#ddlNumbers').showHideDropdownOptions('3', false);
});
$.fn.showHideDropdownOptions = function(value, canShowOption) {
$(this).find('option[value="' + value + '"]').map(function () {
return $(this).parent('span').length === 0 ? this : null;
}).wrap('<span>').hide();
if (canShowOption)
$(this).find('option[value="' + value + '"]').unwrap().show();
else
$(this).find('option[value="' + value + '"]').hide();
}
})(jQuery);
Here is the complete implementation http://jsfiddle.net/8uxD7/3/
这是完整的实现http://jsfiddle.net/8uxD7/3/
#3
1
Updated the 3rd select
更新了第3个选择
<select class="size" id="select_3" name="size">
<option selected="selected" value=""> Choose Size </option>
<!-- Mens Sizes Below -->
<option class="men" value="36">36</option>
<option class="men" value="38">38</option>
<option class="men" value="40">40</option>
<!-- Womens Sizes Below -->
<option class="women" value="30">30</option>
<option class="women" value="29">29</option>
<option class="women" value="28">28</option>
<!-- Children Sizes Below -->
<option class="children" value="12">12</option>
<option class="children" value="11">11</option>
<option class="children" value="10">10</option>
</select>
jquery code.
jquery代码。
$("#select_1").change(function(){
var selectedVal = $(this).val();
if(selectedVal == "Mens Suits")
{
$(".women,.children").hide();
$(".men").show();
}
else if(selectedVal == "Womens Suit")
{
$(".men,.children").hide();
$(".women").show();
}
else
{
$(".women,.children").hide();
$(".children").show();
}
});
#4
1
There are several ways you can achieve that. From your example i can see that you only require 2 out of the 3 dropdown options to be filtering. Is this correct?
有几种方法可以实现这一目标。从您的示例中我可以看到您只需要3个下拉选项中的2个进行过滤。它是否正确?
Please have a look HERE as it will cover many more scenarios that just the one you are mentioning.
请看一下这里,因为它将涵盖更多您提到的场景。
An example with 3 dropdown options that filter each other: EXAMPLE
一个包含3个下拉选项的示例,它们相互过滤:示例
Hope this helps
#5
1
Use selectedIndex property to hide show third selection box
使用selectedIndex属性隐藏显示第三个选择框
$("#select_1").on('change',function(){
if($("#select_1").prop("selectedIndex")==1){
$('#select_3 option').show();
$('#select_3 option:gt(3)').hide();
//$('#select_3 option:lt(3)').show();
}else if($("#select_1").prop("selectedIndex")==2){
$('#select_3 option').show();
$('#select_3 option:lt(4)').hide();
$('#select_3 option:gt(6)').hide();
}else if($("#select_1").prop("selectedIndex")==3){
$('#select_3 option').show();
$('#select_3 option:lt(7)').hide();
}
});
演示
#6
0
Try this:
尝试这个:
Html:
HTML:
<select class="product" id="select_1" name="product">
<option selected="selected" value=""> Choose Category </option>
<option value="Mens Suits"> Mens Suits </option>
<option value="Womens Suit"> Womens Suits </option>
<option value="Children Suit"> Children Suits </option>
</select>
<select class="color" id="select_2" name="color">
<option selected="selected" value=""> Choose Color </option>
<option value="Blue">Blue</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
</select>
<select class="size" id="select_3" name="size">
<option selected="selected" value=""> Choose Size </option>
<!-- Mens Sizes Below -->
<option value="Mens_36">36</option>
<option value="Mens_38">38</option>
<option value="Mens_40">40</option>
<!-- Womens Sizes Below -->
<option value="Womens_30">30</option>
<option value="Womens_29">29</option>
<option value="Womens_28">28</option>
<!-- Children Sizes Below -->
<option value="Children_12">12</option>
<option value="Children_11">11</option>
<option value="Children_10">10</option>
</select>
JQuery:
JQuery的:
$(document).ready(function() {
$("#select_3").children('option:gt(0)').hide();
$("#select_1").change(function() {
$("#select_3").children('option').hide();
$("#select_3").children("option[value^=" + $(this).val().split(" ")[0] + "]").show()
})
})
Working Fiddle
工作小提琴
#7
0
You can use css classes to distinguish the Sizes and show hide the options based on the category.
您可以使用css类来区分大小,并显示基于类别隐藏选项。
<select class="product" id="select_1" name="product">
<option selected="selected" value="">Choose Category </option>
<option value="Mens Suits">Mens Suits </option>
<option value="Womens Suit">Womens Suits </option>
<option value="Children Suit">Children Suits </option>
</select>
<select class="color" id="select_2" name="color">
<option selected="selected" value="">Choose Color </option>
<option value="Blue">Blue</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
</select>
<select class="size" id="select_3" name="size">
<option selected="selected" value="">Choose Size </option>
<!-- Mens Sizes Below -->
<option value="36" class="men">36</option>
<option value="38" class="men">38</option>
<option value="40" class="men">40</option>
<!-- Womens Sizes Below -->
<option value="30" class ="women">30</option>
<option value="29" class ="women">29</option>
<option value="28" class ="women">28</option>
<!-- Children Sizes Below -->
<option value="12" class ="child">12</option>
<option value="11" class ="child">11</option>
<option value="10" class ="child">10</option>
</select>
The above code having classes like "men", "women" and "child"` to distinguish the sizes.
上面的代码有类似“男人”,“女人”和“孩子”的类来区分大小。
Following script can be used to achieve the functionality.
以下脚本可用于实现功能。
<script>
$(document).ready(function () {
$("select#select_1").on('change', function () {
if ($(this).val() == "Mens Suits") {
// Default show the Choose Size Option
$('#select_3 option:first').attr('selected', 'selected');
$(".men").show();
$(".women").hide();
$(".child").hide();
} else if ($(this).val() == "Womens Suit") {
$('#select_3 option:first').attr('selected', 'selected');
$(".women").show();
$(".men").hide();
$(".child").hide();
} else if ($(this).val() == "Children Suit") {
$('#select_3 option:first').attr('selected', 'selected');
$(".child").show();
$(".women").hide();
$(".men").hide();
}
});
});
</script>
Hope this will help :)
希望这会有所帮助:)
#1
5
Try:
尝试:
var men = '<option selected="selected" value=""> Choose Size </option><option value="36">36</option><option value="38">38</option><option value="40">40</option>';
var women = '<option selected="selected" value=""> Choose Size </option><option value="30">30</option><option value="29">29</option><option value="28">28</option>';
var children = '<option selected="selected" value=""> Choose Size </option><option value="12">12</option><option value="11">11</option><option value="10">10</option>';
$(document).ready(function(){
$("select#select_1").on('change',function(){
if($(this).val()=="Mens Suits"){
$("select#select_3").html(men);
}else if($(this).val()=="Womens Suit"){
$("select#select_3").html(women);
}else if($(this).val()=="Children Suit"){
$("select#select_3").html(children);
}
});
});
DEMO FIDDLE
#2
2
I tried in many different ways but this solution seems reasonable and I have used in my code. No plugins required simple register function with jquery object
我试过很多不同的方法,但这个解决方案似乎很合理,我在我的代码中使用过。没有插件需要使用jquery对象的简单注册函数
Solution at glance:
解决方案一览:
(function ($) {
$('#showOne').click(function () {
$('#ddlNumbers').showHideDropdownOptions('3', true);
});
$('#hideOne').click(function () {
$('#ddlNumbers').showHideDropdownOptions('3', false);
});
$.fn.showHideDropdownOptions = function(value, canShowOption) {
$(this).find('option[value="' + value + '"]').map(function () {
return $(this).parent('span').length === 0 ? this : null;
}).wrap('<span>').hide();
if (canShowOption)
$(this).find('option[value="' + value + '"]').unwrap().show();
else
$(this).find('option[value="' + value + '"]').hide();
}
})(jQuery);
Here is the complete implementation http://jsfiddle.net/8uxD7/3/
这是完整的实现http://jsfiddle.net/8uxD7/3/
#3
1
Updated the 3rd select
更新了第3个选择
<select class="size" id="select_3" name="size">
<option selected="selected" value=""> Choose Size </option>
<!-- Mens Sizes Below -->
<option class="men" value="36">36</option>
<option class="men" value="38">38</option>
<option class="men" value="40">40</option>
<!-- Womens Sizes Below -->
<option class="women" value="30">30</option>
<option class="women" value="29">29</option>
<option class="women" value="28">28</option>
<!-- Children Sizes Below -->
<option class="children" value="12">12</option>
<option class="children" value="11">11</option>
<option class="children" value="10">10</option>
</select>
jquery code.
jquery代码。
$("#select_1").change(function(){
var selectedVal = $(this).val();
if(selectedVal == "Mens Suits")
{
$(".women,.children").hide();
$(".men").show();
}
else if(selectedVal == "Womens Suit")
{
$(".men,.children").hide();
$(".women").show();
}
else
{
$(".women,.children").hide();
$(".children").show();
}
});
#4
1
There are several ways you can achieve that. From your example i can see that you only require 2 out of the 3 dropdown options to be filtering. Is this correct?
有几种方法可以实现这一目标。从您的示例中我可以看到您只需要3个下拉选项中的2个进行过滤。它是否正确?
Please have a look HERE as it will cover many more scenarios that just the one you are mentioning.
请看一下这里,因为它将涵盖更多您提到的场景。
An example with 3 dropdown options that filter each other: EXAMPLE
一个包含3个下拉选项的示例,它们相互过滤:示例
Hope this helps
#5
1
Use selectedIndex property to hide show third selection box
使用selectedIndex属性隐藏显示第三个选择框
$("#select_1").on('change',function(){
if($("#select_1").prop("selectedIndex")==1){
$('#select_3 option').show();
$('#select_3 option:gt(3)').hide();
//$('#select_3 option:lt(3)').show();
}else if($("#select_1").prop("selectedIndex")==2){
$('#select_3 option').show();
$('#select_3 option:lt(4)').hide();
$('#select_3 option:gt(6)').hide();
}else if($("#select_1").prop("selectedIndex")==3){
$('#select_3 option').show();
$('#select_3 option:lt(7)').hide();
}
});
演示
#6
0
Try this:
尝试这个:
Html:
HTML:
<select class="product" id="select_1" name="product">
<option selected="selected" value=""> Choose Category </option>
<option value="Mens Suits"> Mens Suits </option>
<option value="Womens Suit"> Womens Suits </option>
<option value="Children Suit"> Children Suits </option>
</select>
<select class="color" id="select_2" name="color">
<option selected="selected" value=""> Choose Color </option>
<option value="Blue">Blue</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
</select>
<select class="size" id="select_3" name="size">
<option selected="selected" value=""> Choose Size </option>
<!-- Mens Sizes Below -->
<option value="Mens_36">36</option>
<option value="Mens_38">38</option>
<option value="Mens_40">40</option>
<!-- Womens Sizes Below -->
<option value="Womens_30">30</option>
<option value="Womens_29">29</option>
<option value="Womens_28">28</option>
<!-- Children Sizes Below -->
<option value="Children_12">12</option>
<option value="Children_11">11</option>
<option value="Children_10">10</option>
</select>
JQuery:
JQuery的:
$(document).ready(function() {
$("#select_3").children('option:gt(0)').hide();
$("#select_1").change(function() {
$("#select_3").children('option').hide();
$("#select_3").children("option[value^=" + $(this).val().split(" ")[0] + "]").show()
})
})
Working Fiddle
工作小提琴
#7
0
You can use css classes to distinguish the Sizes and show hide the options based on the category.
您可以使用css类来区分大小,并显示基于类别隐藏选项。
<select class="product" id="select_1" name="product">
<option selected="selected" value="">Choose Category </option>
<option value="Mens Suits">Mens Suits </option>
<option value="Womens Suit">Womens Suits </option>
<option value="Children Suit">Children Suits </option>
</select>
<select class="color" id="select_2" name="color">
<option selected="selected" value="">Choose Color </option>
<option value="Blue">Blue</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
</select>
<select class="size" id="select_3" name="size">
<option selected="selected" value="">Choose Size </option>
<!-- Mens Sizes Below -->
<option value="36" class="men">36</option>
<option value="38" class="men">38</option>
<option value="40" class="men">40</option>
<!-- Womens Sizes Below -->
<option value="30" class ="women">30</option>
<option value="29" class ="women">29</option>
<option value="28" class ="women">28</option>
<!-- Children Sizes Below -->
<option value="12" class ="child">12</option>
<option value="11" class ="child">11</option>
<option value="10" class ="child">10</option>
</select>
The above code having classes like "men", "women" and "child"` to distinguish the sizes.
上面的代码有类似“男人”,“女人”和“孩子”的类来区分大小。
Following script can be used to achieve the functionality.
以下脚本可用于实现功能。
<script>
$(document).ready(function () {
$("select#select_1").on('change', function () {
if ($(this).val() == "Mens Suits") {
// Default show the Choose Size Option
$('#select_3 option:first').attr('selected', 'selected');
$(".men").show();
$(".women").hide();
$(".child").hide();
} else if ($(this).val() == "Womens Suit") {
$('#select_3 option:first').attr('selected', 'selected');
$(".women").show();
$(".men").hide();
$(".child").hide();
} else if ($(this).val() == "Children Suit") {
$('#select_3 option:first').attr('selected', 'selected');
$(".child").show();
$(".women").hide();
$(".men").hide();
}
});
});
</script>
Hope this will help :)
希望这会有所帮助:)