1 回答

TA貢獻1807條經驗 獲得超9個贊
問題出在你的hideSelects()功能上。它隱藏了所有不是由函數確定的新選擇的值的選擇switchRole()。
假設您只想隱藏當前選擇的同級選擇。幸運的是,jQuery 讓這一切變得非常簡單。在下面的片段中,我替換了:
var elements = $("select").filter(function() {
和
var elements = $(selected).siblings().filter(function() {
它似乎按預期工作。這是假設它確實是您想要隱藏的兄弟選擇,并且它們將始終是您的 html 中的兄弟。希望有幫助!
$('.selectdata').change(function() {
var role = $(this).val();
switchRole(role);
});
function switchRole(role) {
var sel = $('select[data-role= ' + role + ' ]');
sel.show();
hideSelects(role, sel)
};
hideSelects = function(role, selected) {
var elements = $(selected).siblings().filter(function() {
return $(this).data("role") !== undefined;
});
var toHide = $(elements).not(selected);
toHide.hide();
};
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card mt-3">
<div class="card-body">
<div class="form-group row">
<label class="col-md-1 m-t-15">Select option:</label>
<!-- OPTIE 1 -->
<div class="col-md-1" style="max-width: 150px;">
<select class="selectdata form-control custom-select" style="width: 100%; height:36px;">
<optgroup label="Select option NIV 1">
<option value="A1">NIV1 A</option>
<option value="A2">NIV1 B</option>
<option value="A3">NIV1 C</option>
</optgroup>
</select>
</div>
<!-- OPTIE 2 -->
<div class="col-md-1" style="max-width: 150px;">
<select data-role="A1" class="selectdata form-control custom-select" style="width: 100%; height:36px;">
<optgroup label="Select option NIV 2">
<option value="B11">A 11</option>
<option value="B12">A 12</option>
<option value="B13">A 13</option>
</optgroup>
</select>
<select data-role="A2" class="selectdata form-control custom-select" style="width: 100%; height:36px; display: none;">
<optgroup label="Select option NIV 2">
<option value="B21">A 21</option>
<option value="B22">A 22</option>
<option value="B23">A 23</option>
</optgroup>
</select>
<select data-role="A3" class="selectdata form-control custom-select" style="width: 100%; height:36px; display: none;">
<optgroup label="Select option NIV 2">
<option value="B31">A 31</option>
<option value="B32">A 32</option>
<option value="B33">A 33</option>
</optgroup>
</select>
</div>
<!-- OPTIE 3 -->
<div class="col-md-1" style="max-width: 150px;">
<select data-role="B11" class="form-control custom-select" style="width: 100%; height:36px; display: none;">
<optgroup label="OPTION NIV 3">
<option value="1">NIV3 A1 - 1</option>
<option value="2">NIV3 A1 - 2</option>
<option value="3">NIV3 A1 - 3</option>
</optgroup>
</select>
<select data-role="B12" class="form-control custom-select" style="width: 100%; height:36px; display: none;">
<optgroup label="OPTION NIV 3">
<option value="1">NIV3 B - 1</option>
<option value="2">NIV3 B - 2</option>
<option value="3">NIV3 B - 3</option>
</optgroup>
</select>
<select data-role="B13" class="form-control custom-select" style="width: 100%; height:36px; display: none;">
<optgroup label="OPTION NIV 3">
<option value="1">NIV3 C - 1</option>
<option value="2">NIV3 C - 2</option>
<option value="3">NIV3 C - 3</option>
</optgroup>
</select>
<select data-role="B21" class="form-control custom-select" style="width: 100%; height:36px; display: none;">
<optgroup label="OPTION NIV 3">
<option value="1">NIV3 A - 1</option>
<option value="2">NIV3 A - 2</option>
<option value="3">NIV3 A - 3</option>
</optgroup>
</select>
<select data-role="B22" class="form-control custom-select" style="width: 100%; height:36px; display: none;">
<optgroup label="OPTION NIV 3">
<option value="1">NIV3 B - 1</option>
<option value="2">NIV3 B - 2</option>
<option value="3">NIV3 B - 3</option>
</optgroup>
</select>
<select data-role="B23" class="form-control custom-select" style="width: 100%; height:36px; display: none;">
<optgroup label="OPTION NIV 3">
<option value="1">NIV3 C - 1</option>
<option value="2">NIV3 C - 2</option>
<option value="3">NIV3 C - 3</option>
</optgroup>
</select>
<select data-role="B31" class="form-control custom-select" style="width: 100%; height:36px; display: none;">
<optgroup label="OPTION NIV 3">
<option value="1">NIV3 A - 1</option>
<option value="2">NIV3 A - 2</option>
<option value="3">NIV3 A - 3</option>
</optgroup>
</select>
<select data-role="B32" class="form-control custom-select" style="width: 100%; height:36px; display: none;">
<optgroup label="OPTION NIV 3">
<option value="1">NIV3 B - 1</option>
<option value="2">NIV3 B - 2</option>
<option value="3">NIV3 B - 3</option>
</optgroup>
</select>
<select data-role="B33" class="form-control custom-select" style="width: 100%; height:36px; display: none;">
<optgroup label="OPTION NIV 3">
<option value="1">NIV3 C - 1</option>
<option value="2">NIV3 C - 2</option>
<option value="3">NIV3 C - 3</option>
</optgroup>
</select>
</div>
</div>
</div>
</div>
添加回答
舉報