JSFiddle上的代码无法在浏览器中运行

时间:2016-11-02 11:24:45

标签: jquery html css

我在jsfiddle中有这段代码:https://jsfiddle.net/2ftk5c8z/402/

当我尝试使用浏览器时,它无效。

    <html>
<head>
<script src="https://cdnjs.cloudflare.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 href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
</head>
<body>
<select class="form-control select2-hidden-accessible" id="ListResort" multiple name="ListResort" tabindex="-1" aria-hidden="true"><option value="">Select Resort</option>
<optgroup label="Hotels &amp; Resorts">
<option value="Miramar by Pestana São Tomé">Miramar by Pestana São Tomé</option>
<option value="Pestana Alvor Atlantico">Pestana Alvor Atlantico</option>
<option value="Pestana Alvor Park">Pestana Alvor Park</option>
<option value="Pestana Alvor Park 2">Pestana Alvor Park 2</option>
<option value="Pestana Alvor Praia">Pestana Alvor Praia</option>
<option value="Pestana Alvor South Beach">Pestana Alvor South Beach</option>
<option value="Pestana Buenos Aires">Pestana Buenos Aires</option>
<option value="Pestana Caracas">Pestana Caracas</option>
<option value="Pestana Carlton Madeira">Pestana Carlton Madeira</option>
<option value="Pestana Casablanca">Pestana Casablanca</option>
<option value="Pestana Cascais">Pestana Cascais</option>
<option value="Pestana Casino Park">Pestana Casino Park</option>
<option value="Pestana Cayo Coco">Pestana Cayo Coco</option>
<option value="Pestana Chelsea Bridge Hotel">Pestana Chelsea Bridge Hotel</option>
</optgroup>
</select>

<script type="text/javascript">
$(function() {

$('#ListResort').select2();
$('#ListResort').select2().select2('val', ["Miramar by Pestana São Tomé", "Pestana Alvor Park 2"]).trigger("change");

  });

</script>
</body>
</html>

我做错了什么?

提前致谢

1 个答案:

答案 0 :(得分:0)

检查您的包含/外部资源。

在小提琴上,你包括3.5.0和4.0.3,但在问题中你只包括4.0.3。

如果您将本地版本更改为3.5.0,它可以正常工作:

<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.css" rel="stylesheet" />

更多信息

{4.0}中删除了.select2("val" ..,替换为.val(...

https://select2.github.io/announcements-4.0.html#removed-methods