无法读取未定义的select2的属性“id”

时间:2018-04-21 07:32:25

标签: javascript jquery html jquery-select2

我使用的是Select2 4.0.6,我有一个如下的错误:

allowClear在select上使用时给出“未捕获的TypeError:无法读取未定义的属性'id'。

如何修复此错误?。

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
	<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
</head>
<body>
	<select role="select" id="myoption">
		<option value="001">abcs </option><option value="002">dshdsh</option><option value="003">A</option>
		<option value="004">ANAM CO</option>
	</select>

</body>
</html>
<script>
 $("#myoption").select2({
        allowClear: true,
        width: '300px',
        height: '34px'
        //data: data
    });
</script>

2 个答案:

答案 0 :(得分:11)

如果您将debug属性设置为true,则会看到警告消息。

  

allowClear选项应与。{   placeholder选项。

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
	<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
</head>
<body>
	<select role="select" id="myoption">
		<option value="001">abcs </option><option value="002">dshdsh</option><option value="003">A</option>
		<option value="004">ANAM CO</option>
	</select>

</body>
</html>
<script>
 $("#myoption").select2({
        allowClear: true,
        width: '300px',
        height: '34px',
        debug: true
        //data: data
    });
</script>
&#13;
&#13;
&#13;

因此,您必须将占位符定义为默认值。

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
	<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
</head>
<body>
	<select role="select" id="myoption">
		<option value="001">abcs </option><option value="002">dshdsh</option><option value="003">A</option>
		<option value="004">ANAM CO</option>
	</select>

</body>
</html>
<script>
 $("#myoption").select2({
        allowClear: true,
        width: '300px',
        height: '34px',
        placeholder :'select..'
        //data: data
    });
</script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

在我看来,data-placehodler帮助:

选择元素:

<select class="form-control select2" data-placeholder="All" required="required" id="manager_id" name="filter[manager_id]" tabindex="-1" aria-hidden="true">
    <option value="1" selected="selected">Manager 1</option>
    <option value="2" selected="selected">Manager 2</option>
</select>

JS:

$('.select2').each(function(){
    var select = $(this);
    select.select2({
         width: '100%',
         allowClear: !select.prop('required'),
         language: {
            noResults: function () {
                return "{{ __('No results found') }}";
            }
        }
    });
});