gpt4 book ai didi

javascript - 根据JS中html标签的类和id(增量)隐藏和显示div

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

目前我有多个使用MDB前端框架的选择选项

我正在使用不同的类、id 和名称加载它

这是工作代码

 @if(count($manage_access_list))
@foreach($manage_access_list as $field)
<div class="row">
<div class="col-md-2">
{{$field->access_desc}}
</div>
<div class="col-md-4">
<select class="mdb-select accessSelect{{$field->id}}" name="accessSelect{{$field->id}}" id="accessSelect{{$field->id}}" style="width: 170px!important;">
<option value="" disabled selected>Select Access Right</option>
<option value="ALL">All Access</option>
<option value="NO">No Access</option>
<option value="CONFIG">Config Access</option>
</select>
</div>

<div class="col-md-2 access-right{{$field->id}}" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input add{{$field->id}}" name="add{{$field->id}}" id="add{{$field->id}}">
<label class="custom-control-label" for="add{{$field->id}}">Add</label>
</div>
</div>
<div class="col-md-2 access-right{{$field->id}}" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input edit{{$field->id}}" name="edit{{$field->id}}" id="edit{{$field->id}}">
<label class="custom-control-label" for="edit{{$field->id}}">Edit</label>
</div>
</div>
<div class="col-md-2 access-right{{$field->id}}" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input remove{{$field->id}}" name="remove{{$field->id}}" id="remove{{$field->id}}">
<label class="custom-control-label" for="remove{{$field->id}}">Remove</label>
</div>
</div>

</div>
@endforeach
@endif

这是我的 JS 代码

<script>
$("#accessSelect1").on('change', function() {
if(this.value == "ALL"){
$(".access-right1").hide();
}
else if (this.value == "NO"){
$(".access-right1").hide();
}
else if(this.value == "CONFIG"){
$(".access-right1").show();
}
});

</script>

输出:

enter image description here

enter image description here

如您所见,select option > accessSelect1 的 id 是硬编码的

我想做的是显示和隐藏每行的特定添加、编辑、删除。使用select option accessSelect

的id

到目前为止,div 的显示和隐藏仅适用于 accessSelect1

最佳答案

// [id^=accessSelect] will select all IDs starting with"accessSelect"
$("[id^=accessSelect]").on('change', function() {
// from the current select get the parent .row element
// from there chose the child containing a class "access-right"
var $accessRights = $(this).closest('.row').children('[class*=access-right]');
if($(this).val() == "CONFIG") {
$accessRights.show();
} else {
// if it's not "CONFIG" you want to hide them allways
$accessRights.hide();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-4">
<select class="mdb-select accessSelect1" name="accessSelect1" id="accessSelect1" style="width: 170px!important;">
<option value="" disabled selected>Select Access Right</option>
<option value="ALL">All Access</option>
<option value="NO">No Access</option>
<option value="CONFIG">Config Access</option>
</select>
</div>

<div class="col-md-2 access-right1" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input add1" name="add1" id="add1">
<label class="custom-control-label" for="add1">Add</label>
</div>
</div>
<div class="col-md-2 access-right1" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input edit1" name="edit1" id="edit1">
<label class="custom-control-label" for="edit1">Edit</label>
</div>
</div>
<div class="col-md-2 access-right1" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input remove1" name="remove1" id="remove1">
<label class="custom-control-label" for="remove1">Remove</label>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<select class="mdb-select accessSelect2" name="accessSelect2" id="accessSelect2" style="width: 170px!important;">
<option value="" disabled selected>Select Access Right</option>
<option value="ALL">All Access</option>
<option value="NO">No Access</option>
<option value="CONFIG">Config Access</option>
</select>
</div>

<div class="col-md-2 access-right2" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input add1" name="add1" id="add1">
<label class="custom-control-label" for="add1">Add</label>
</div>
</div>
<div class="col-md-2 access-right2" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input edit1" name="edit1" id="edit1">
<label class="custom-control-label" for="edit1">Edit</label>
</div>
</div>
<div class="col-md-2 access-right2" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input remove1" name="remove1" id="remove1">
<label class="custom-control-label" for="remove1">Remove</label>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<select class="mdb-select accessSelect3" name="accessSelect3" id="accessSelect3" style="width: 170px!important;">
<option value="" disabled selected>Select Access Right</option>
<option value="ALL">All Access</option>
<option value="NO">No Access</option>
<option value="CONFIG">Config Access</option>
</select>
</div>

<div class="col-md-2 access-right3" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input add1" name="add1" id="add1">
<label class="custom-control-label" for="add1">Add</label>
</div>
</div>
<div class="col-md-2 access-right3" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input edit1" name="edit1" id="edit1">
<label class="custom-control-label" for="edit1">Edit</label>
</div>
</div>
<div class="col-md-2 access-right3" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input remove1" name="remove1" id="remove1">
<label class="custom-control-label" for="remove1">Remove</label>
</div>
</div>
</div>

<div class="row">
<div class="col-md-4">
<select class="mdb-select accessSelect4" name="accessSelect4" id="accessSelect4" style="width: 170px!important;">
<option value="" disabled selected>Select Access Right</option>
<option value="ALL">All Access</option>
<option value="NO">No Access</option>
<option value="CONFIG">Config Access</option>
</select>
</div>

<div class="col-md-2 access-right4" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input add1" name="add1" id="add1">
<label class="custom-control-label" for="add1">Add</label>
</div>
</div>
<div class="col-md-2 access-right4" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input edit1" name="edit1" id="edit1">
<label class="custom-control-label" for="edit1">Edit</label>
</div>
</div>
<div class="col-md-2 access-right4" style="display:none;">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input remove1" name="remove1" id="remove1">
<label class="custom-control-label" for="remove1">Remove</label>
</div>
</div>
</div>

关于javascript - 根据JS中html标签的类和id(增量)隐藏和显示div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59818680/

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