隐藏 select2 多搜索框
Hide select2 multi search box
如果 HTML 下拉列表中的值与某个值匹配,我试图使 select2 搜索框可见。以下是我的代码片段。
不知何故,始终显示 Select2 下拉列表。
感谢任何帮助...
我根据 select2 github 回购的建议添加了 CSS
https://github.com/select2/select2/issues/861
<html>
<style type="text/css">
.other {
display: none !important;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>
<select class="one" name="one" id="one">
<option value="1">1</option>
<option value="2">3</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<br>
<br>
<select class="other" name="other" multiple="multiple">
<option value="AL">Sample1</option>
<option value="WY">Sample2</option>
</select>
<script>
// In your Javascript (external .js resource or <script> tag)
$("#one").change(function() {
$('#value').change(function()
{
if($('#value').val() == '1')
{
$('.other').hide();
} else
{
$('.other').show();
}
}
)});
$(document).ready(function() {
$(".other").select2({
tags: true
});
});
</script>
</html>
$("#one").change(function() {
console.log('me called');
if($('#one').val() == '1')
{
$('.select2').hide();
console.log($('#other'))
console.log('me called1');
} else
{
$('.select2').show();
console.log('me called2');
}
});
这对你有用吗?
当您 select 3 然后 select 1 时,select2 元素消失。
你能做这样的事情吗?不确定您的用户案例。但我会建议销毁 select 然后隐藏。稍后再次创建实例并显示。
$("#one").change(function() {
if($(this).val() == '3')
{
$('.other').select2('destroy');
$('.other').hide();
} else {
$(".other").select2({
tags: true
});
$('.other').show();
}
});
$(document).ready(function() {
$(".other").select2({
tags: true
});
});
<html>
<style type="text/css">
.other {
display: none !important;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>
<select class="one" name="one" id="one">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<br>
<br>
<select class="other" name="other" multiple="multiple" style="width: 100px;">
<option value="AL">Sample1</option>
<option value="WY">Sample2</option>
</select>
</html>
我根据 select2 github 回购的建议添加了 CSS https://github.com/select2/select2/issues/861
<html>
<style type="text/css">
.other {
display: none !important;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>
<select class="one" name="one" id="one">
<option value="1">1</option>
<option value="2">3</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<br>
<br>
<select class="other" name="other" multiple="multiple">
<option value="AL">Sample1</option>
<option value="WY">Sample2</option>
</select>
<script>
// In your Javascript (external .js resource or <script> tag)
$("#one").change(function() {
$('#value').change(function()
{
if($('#value').val() == '1')
{
$('.other').hide();
} else
{
$('.other').show();
}
}
)});
$(document).ready(function() {
$(".other").select2({
tags: true
});
});
</script>
</html>
$("#one").change(function() {
console.log('me called');
if($('#one').val() == '1')
{
$('.select2').hide();
console.log($('#other'))
console.log('me called1');
} else
{
$('.select2').show();
console.log('me called2');
}
});
这对你有用吗? 当您 select 3 然后 select 1 时,select2 元素消失。
你能做这样的事情吗?不确定您的用户案例。但我会建议销毁 select 然后隐藏。稍后再次创建实例并显示。
$("#one").change(function() {
if($(this).val() == '3')
{
$('.other').select2('destroy');
$('.other').hide();
} else {
$(".other").select2({
tags: true
});
$('.other').show();
}
});
$(document).ready(function() {
$(".other").select2({
tags: true
});
});
<html>
<style type="text/css">
.other {
display: none !important;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>
<select class="one" name="one" id="one">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<br>
<br>
<select class="other" name="other" multiple="multiple" style="width: 100px;">
<option value="AL">Sample1</option>
<option value="WY">Sample2</option>
</select>
</html>