gpt4 book ai didi

javascript - Laravel 5 如何通过下拉选择显示、隐藏 div

转载 作者:行者123 更新时间:2023-11-28 00:42:42 25 4
gpt4 key购买 nike

Problem is how to show and hide div by using dropdown onclick function.

Blockquote Problem 2 is how to take getElementById("") by that style's number;

这是我的 CSS,我不想显示所有的 div

<style>
#\1a{ display:none; } // to 32...//
</style>

这是我的JS

<script>
function show(showid){
document.getElementById("1").style.display="none";
document.getElementById("2").style.display="none";
document.getElementById("3").style.display="none";
}
</script>

这是我的下拉菜单

<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" >
Bla bla bla
</button>
<ul id="test" name="form_select" class="dropdown-menu">
@foreach ($topics as $topic)
<li><a href="#" onclick="show('')" style="font-size: 11px; padding:0px; line-height: 15px;">{{ $topic->title }}</a></li>
@endforeach
</ul>
</div>

这是我的div

@foreach($collections as $collection)
<div id="{{ $collection->id }}">
<div class="panel panel-default">
<table border="1" style="width:100%">
<tbody>
<tr>
<td><h4>Асуулт {{ $i++ }}.</h4><strong>{!! nl2br($question->question_text) !!}</strong></td>
</tr>
<tr>
<td><input type="hidden" class="form-control" name="questions[{{ $question->id }}]" value="{{ $question->id }}"></td>
</tr>
@foreach($question->options as $option)
<tr>
<td><label class="radio-inline"><input type="radio" name="answers[{{ $question->id }}]" value="{{ $option->id }}">{{ $option->option }}</label>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>

最佳答案

下拉列表:

<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true">
Bla bla bla
</button>
<ul id="test" name="form_select" class="dropdown-menu">
@foreach ($topics as $topic)
<li><a href="#" onclick="show({{ $loop->index }})" style="font-size: 11px; padding:0px; line-height: 15px;">{{ $topic->title }}</a></li>
@endforeach
</ul>
</div>

我们使用 $loop->index作为增量值。它们必须等于 $collection->id

DIV:

@foreach($collections as $collection)
<div id="{{ $collection->id }}" class="topic hidden">
<div class="panel panel-default">
...
</div>
</div>
@endforeach

我添加了类 topic 来识别所有 div(使用任何你想要的)并用 hidden 隐藏它们类(class)。无需自定义 CSS

JavaScript:

function show(id) {
$('.topic').addClass('hidden');
$('#' + id).removeClass('hidden');
}

第一行将类hidden 添加到所有div 以隐藏它们。第二个删除特定 divhidden 类。
我使用了 jQuery,因为您正在将该库用于 Bootstrap

This all assumes that $loop->index and $collection->id have the same values. If not, you could try replacing $collection->id with $loop->index.

关于javascript - Laravel 5 如何通过下拉选择显示、隐藏 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52489718/

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