jquery – 占位符不在select2中工作

前端之家收集整理的这篇文章主要介绍了jquery – 占位符不在select2中工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Select2选择框.

问题

占位符未显示在select2中.它始终显示在select2中选择的第一个选项.它会自动选择我想要显示占位符而不是它的第一个选项.

我的代码

脚本:

<script type="text/javascript">
    $(document).ready(function () {
       var data = $('#test_skill').select2({
            placeholder: "Please select an skill",allowClear: true
       });
    });

// I have also tried this: This is also not working
    $('#test_skill').select2({
      placeholder: {
        id: '-1',// the value of the option
        text: 'Please select an skill'
      } 
    });
</script>

HTML:

<select class="skills_select2" required name="test_skill" id="test_skill">          
    <option value="1">TEST1</option>
    <option value="2">TEST2</option>
    <option value="3">TEST3</option>            
</select>

解决方法

只需输入< option>< / option>在第一名选择:
$(document).ready(function() {
  $selectElement = $('#test_skill').select2({
    placeholder: "Please select an skill",allowClear: true
  });
});
.skills_select2 {
  width: 120px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css" />
<select class="skills_select2" required name="test_skill" id="test_skill">   
    <option></option>
    <option value="1">TEST1</option>
    <option value="2">TEST2</option>
    <option value="3">TEST3</option>            
</select>
原文链接:https://www.f2er.com/jquery/176896.html

猜你在找的jQuery相关文章