Multiple dependent select box on page jquery

2019-04-12 13:37发布

问题:

Hello I'm trying to create form where are two or more same dependent select boxes. Problem is that I can choose from first select box and can't figure out how to choose from other(s). Can anyone help me with this.

I can't select from third select box.

Here is: JSFiddle

JS code:

        $(document).ready(function() {
      $('select[name*="[]"]').each(function(){

        var attribute = {
        'shoes': ['standard'],
        'trous': ['male', 'female'],
        'shirt': ['blue', 'red', 'green', 'brown', 'yellow'],
        'hoodie': ['blue', 'red'],
        }

        var size = {
            'shoes': ['35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46'],
            'trous': ['44', '46', '48', '50', '52', '54', '56', '58', '60', '62', '64'],
            'shirt': ['S', 'M', 'L', 'XL', 'XXL'],
            'hoodie': ['S', 'M', 'L', 'XL', 'XXL'],
        }

        $('select[name*="[]"]').change(function () {
        var $attribute = $(this).next('.attribute');
        var product = $(this).val(), attProd = attribute[product] || [];

        var html = $.map(attProd, function (attOpt) {
            return '<option value="' + attOpt + '">' + attOpt + '</option>'
        }).join('');
        $attribute.html(html)
       });

        ('select[name*="[]"]').change(function () {
        var $size = $('select').next('.size');
        var product = $(this).val(), sizeProd = size[product] || [];

        var htmlS = $.map(sizeProd, function(sProd){
            return '<option value="' + sProd + '">' + sProd + '</option>'
        }).join('');
        $size.html(htmlS)
        });
       });
    });

and html code here:

<div class="options">
    <select name="product[]" class="custom-select form-control-sm product">
        <option></option>
        <option value="shoes">Boots</option>
        <option value="trous">Trousers</option>
        <option value="shirt">Shirt</option>
        <option value="hoodie">Hoodie</option>
    </select>
    <select name="att[]" class="custom-select form-control-sm attribute">
    </select>
    <select name="size[]" class="custom-select form-control-sm size">
    </select>
   <input name="number[]" type="text" style="width: 50px"/>
</div>
<div class="options">
    <select name="product[]" class="custom-select form-control-sm product">
        <option></option>
        <option value="shoes">Boots</option>
        <option value="trous">Trousers</option>
        <option value="shirt">Shirt</option>
        <option value="hoodie">Hoodie</option>
    </select>
    <select name="att[]" class="custom-select form-control-sm attribute">
    </select>
    <select name="size[]" class="custom-select form-control-sm size">
    </select>
   <input name="number[]" type="text" style="width: 50px"/>
</div>

回答1:

First of all your html code is going wrong cause you're using two select with same id:

<select name="att[]" id="attribute" class="custom-select form-control-sm attribute">

<select name="product[]" id="product" class="custom-select form-control-sm product">

Then I correct your I correct your JSFiddle here & it's working fine

$(document).ready(function(){
         $('select[name*="[]"]').each(function(){
             
            var attribute = {
            'shoes': ['standard'],
            'trous': ['male', 'female'],
            'shirt': ['blue', 'red', 'green', 'brown', 'yellow'],
            'hoodie': ['blue', 'red'],
            }
					
            
                        
 var size = {
            'shoes': ['35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46'],
            'trous': ['44', '46', '48', '50', '52', '54', '56', '58', '60', '62', '64'],
            'shirt': ['S', 'M', 'L', 'XL', 'XXL'],
            'hoodie': ['S', 'M', 'L', 'XL', 'XXL'],
        }


            $('select[name*="[]"]').change(function () {
            var $attribute = $(this).next('.attribute');
             var $size = $(this).next('.attribute').next('.size');
             
            var product = $(this).val(), lcns = attribute[product] || [];
	          var product22 = $(this).val(), lcns22 = size[product] || [];
            
            var html = $.map(lcns, function(lcn){
                return '<option value="' + lcn + '">' + lcn + '</option>'
            }).join('');
           
              
            var html2 = $.map(lcns22, function(lcn){
                return '<option value="' + lcn + '">' + lcn + '</option>'
            }).join('');
           
            $attribute.html(html);
            $size.html(html2);
            });
        });
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="options">
    <select name="product[]" class="custom-select form-control-sm product">
        <option></option>
        <option value="shoes">Boots</option>
        <option value="trous">Trousers</option>
        <option value="shirt">Shirt</option>
        <option value="hoodie">Hoodie</option>
    </select>
    <select name="att[]" class="custom-select form-control-sm attribute">
    </select>
    <select name="size[]" class="custom-select form-control-sm size">
    </select>
   <input name="number[]" type="text" style="width: 50px"/>
</div>
<div class="options">
    <select name="product[]" class="custom-select form-control-sm product">
        <option></option>
        <option value="shoes">Boots</option>
        <option value="trous">Trousers</option>
        <option value="shirt">Shirt</option>
        <option value="hoodie">Hoodie</option>
    </select>
    <select name="att[]" class="custom-select form-control-sm attribute">
    </select>
    <select name="size[]" class="custom-select form-control-sm size">
    </select>
   <input name="number[]" type="text" style="width: 50px"/>
</div>



回答2:

Hope the below code works for you. The id for each HTML element should be unique and so I removed id=product and id=attribute for select elements. Updated JS code to get handle to attribute select as follows:

$(this).parent().siblings().find(".attribute");

$(document).ready(function () {
	$('.product').each(function () {

		var attribute = {
			'shoes': ['standard'],
			'trous': ['male', 'female'],
			'shirt': ['blue', 'red', 'green', 'brown', 'yellow'],
			'hoodie': ['blue', 'red'],
		}

		$('.product').change(function () {
			var $attribute = $(this).parent().siblings().find(".attribute");
			var product = $(this).val(), lcns = attribute[product] || [];

			var html = $.map(lcns, function (lcn) {
				return '<option value="' + lcn + '">' + lcn + '</option>'
			}).join('');
			$attribute.html(html)
		});
	});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<form class="input" action="test.php" method="post">

<div class="">
	<span>
		<select name="product[]" class="custom-select form-control-sm product">
			<option></option>
			<option value="shoes">Boots</option>
			<option value="trous">Trousers</option>
			<option value="shirt">Shirt</option>
			<option value="hoodie">Hoodie</option>
		</select>
	</span>
	<span>
		<select name="att[]" class="custom-select form-control-sm attribute">
		</select>
	</span>
	<span><input name="number" type="text" style="width: 50px" /></span>
</div>

<div class="">
	<span>
		<select name="product[]" class="custom-select form-control-sm product">
			<option></option>
			<option value="shoes">Boots</option>
			<option value="trous">Trousers</option>
			<option value="shirt">Shirt</option>
			<option value="hoodie">Hoodie</option>
		</select>
	</span>
	<span>
		<select name="att[]" class="custom-select form-control-sm attribute">
		</select>
	</span>
	<span><input name="number" type="text" style="width: 50px" /></span>
</div>

<button type="submit" class="btn btn-primary">Submit</button>
</form>