select2:无法读取未定义的属性“id" [英] select2: cannot read property 'id' of undefined

查看:25
本文介绍了select2:无法读取未定义的属性“id"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Select2 4.0.6,我有一个类似下面的错误:

I used Select2 4.0.6, I have a bug like below:

allowClear 给出未捕获的类型错误:无法读取未定义的属性 'id'";用于选择时.

allowClear gives "Uncaught TypeError: Cannot read property 'id' of undefined" when used on select.

我该如何修复这个错误?

How can I fix this bug?.

<!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>

推荐答案

如果您将 debug 属性设置为 true,您将看到一条警告消息.

If you set the debug property to true you'll see a warning message.

allowClear 选项应与placeholder 选项.

<!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>

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

So you have to define a placeholder as default.

<!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>

这篇关于select2:无法读取未定义的属性“id"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆