您好,我正在尝试创建表单,其中有两个或更多相同的依赖选择框。问题是我可以从第一个选择框中进行选择,但我不知道如何从其他选择框中进行选择。谁能帮我做这个。
JSFiddle
JS代码:
$(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)
});
});
});
和html代码:
<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>