gpt4 book ai didi

jquery - jquery页面上的多个依赖选择框

转载 作者:行者123 更新时间:2023-12-01 01:13:27 25 4
gpt4 key购买 nike

您好,我正在尝试创建表单,其中有两个或多个相同的依赖选择框。问题是我可以从第一个选择框中进行选择,但无法弄清楚如何从其他选择框中进行选择。谁能帮我解决这个问题。

我无法从第三个选择框中进行选择。

这里是: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>

最佳答案

首先,您的 html 代码出错,因为您使用了两个具有相同 id 的 select:

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

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

然后我纠正你我纠正你JSFiddle在这里并且工作正常

$(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>

关于jquery - jquery页面上的多个依赖选择框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46343301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com