当我下载Select2 的4.0.1 Release Candidate(https://github.com/select2/select2/releases/tag/4.0.1-rc.1)并使用它时,我收到一个JavaScript错误:Uncaught ReferenceError: define is not defined.
在这里我读到它与AMD加载器(我不使用)有关:https://groups.google.com/forum/#!topic/select2/PCQpiJxIIXQ
当我不使用来自RC的jquery.select2.js,而是来自当前主服务器的select2.min.js时:https://raw.githubusercontent.com/select2/select2/master/dist/js/select2 . min.js
有用!
但我很好奇我在这里忽略了什么.我很可能自己做错了什么.
编辑 - 代码示例:
在脑袋里:
<script language='Javascript' src='scripts/jquery-1.9.1.min.js'></script>
<script language='Javascript' src='scripts/jquery.select2.js'></script>
Run Code Online (Sandbox Code Playgroud)
在身体里:
<script type="text/javascript">
$(document).ready(function(){
$('#id_of_select').select2({
tags: "true",
placeholder: "",
allowClear: true,
tokenSeparators: [',', ' ']
});
});
</script>
<select multiple id="id_of_select" name="name[]">
<option value="1" selected="">Option 1</option>
<option value="2" selected="">Option 2</option>
<option value="5">Option 3</option>
</select>
Run Code Online (Sandbox Code Playgroud) 我已经使用Select2 4.0.0-rc.1几周了(使用ajax适配器),我试图在初始化之后找到一种"推送"数据的方法.
在下拉列表中,我可以选择
ajax)createTag)如果我选择"添加新条目",我可以填写表格,一旦保存,新数据必须显示为选定条目.
如果我使用select2_existing.select2( { data: data } ).val( 4 );它推送数据有效,但ajax呼叫不再起作用.
我已经到了
这将允许我让我的新数据和ajax适配器工作.
没有create-> data-> destroy-> create cycle就可以做到这一点?
我试图根据这里的一个例子创建一个自定义数据适配器:http://select2.github.io/announcements-4.0.html#query-to-data-adapter.如何使用DataAdapter的定义将创建select2控件的行移动到函数外部(请参阅下面的代码)?
<!DOCTYPE html>
<head>
<title></title>
<link href="select2.css" rel="stylesheet" />
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.js"></script>
<script type="text/javascript" src="select2.full.js"></script>
<script type="text/javascript">
$.fn.select2.amd.require(
['select2/data/array', 'select2/utils'],
function (ArrayData, Utils) {
function CustomData ($element, options) {
CustomData.__super__.constructor.call(this, $element, options);
}
Utils.Extend(CustomData, ArrayData);
CustomData.prototype.query = function (params, callback) {
var data = {results: []};
data.results.push({id: params.term, text: params.term});
data.results.push({id: 11, text: 'aa'});
data.results.push({id: 22, text: 'bb'});
callback(data);
};
// Works if uncommented, but this line needs to be elsewhere (in $(document).ready()).
//$("#my").select2({tags: true, dataAdapter: …Run Code Online (Sandbox Code Playgroud) 我使用的是最新版本的 Select2。想要在下拉列表的末尾添加一个插入新按钮。我尝试了网上找到的这两种解决方案
解决方案1:
$(".select2-drop").append('<table width="100%"><tr><td class="row"><button class="btn btn-block btn-default btn-xs" onClick="modal()">Add new Item</button></div></td></tr></table>');
Run Code Online (Sandbox Code Playgroud)
解决方案2
$("#itemId0").select2("container").find("div.select2-drop").append('<table width="100%"><tr><td class="row"><button class="btn btn-block btn-default btn-xs" onClick="modal()">Add new Item</button></div></td></tr></table>');
Run Code Online (Sandbox Code Playgroud)
使用解决方案 1 没有任何反应。使用解决方案 2 我在选择 2 js 文件中收到错误消息
0x800a138f - JavaScript 运行时错误:无法获取未定义或空引用的属性“应用”
有人可以帮忙吗?
这是我的 HTML
<select id='itemId0' name='product[0][name]' class='form-control col-lg-5 itemSearch' >
<option></option>
</select>
Run Code Online (Sandbox Code Playgroud)
和完整的 select2 javascript
function productFormatResult(product) {
if (product.loading) product.text;
var html = "<table><tr>";
html += "<td>";
html += product.text;
html += "</td></tr></table>";
return html;
}
// alert(html);
function productFormatSelection(product) {
var selected …Run Code Online (Sandbox Code Playgroud) 在我的项目中,我检查是否以这种方式加载了Select2插件
if (jQuery().select2)
Run Code Online (Sandbox Code Playgroud)
但现在我将尝试验证加载了哪种版本(3.5.X或4.X)的Select2插件.我想也许你可以检查版本4.X中是否有版本3.5.X中没有的选项/功能.据你说,它可行吗?我怎么能这样做?谢谢
我已经实现了select2小部件,版本4.它可以工作,但是x图标.它没有清除选择.
如果您看到此文档:https://select2.github.io/options.html,它说这实际上是一个问题,但文档对此不完整.
有谁已经解决了这个问题?
谢谢Jaime
如何在select2capybara中填写/选择一个搜索框(通过ajax获取结果).
使用最新version 4的select2和最新capybara/rspec的rails项目.
SO关于如何使用Capybara 有很多例子和其他地方,Select2 3.x但不是version 4重写.
尝试使用select2插件制作两个链式自动填充选择字段第一个选择包含国家名称(静态选择),第二个选择显示从第一个选择我的代码中选择的国家/地区的状态是
<select id="country" name="country" size="1" class=" form-control">
<option></option>
<option value="1">USA</option>
<option value="2">Untied Kingdom</option>
<option value="3">France</option>
etc........
</select>
<select id="states" name="states" size="1" class="form-control">
<option></option>
</select>
Run Code Online (Sandbox Code Playgroud)
JavaScript代码
$(document).ready(function() {
$('#country').select2({
placeholder: 'Select Country',
allowClear: true
});
$('#state').select2({
placeholder: 'Select State',
allowClear: true
});
$('#country').change(function() {
var selectedCountries = $('#countryoption:selected').val();
var selectedCountries = 'id='+ selectedCountries;
$.ajax({
type: 'POST',
url: 'http://localhost/CodeIgniter/countries/get_state/',
dataType: 'html',
data: selectedCountries,
}).done( function( data ) {
data = $.map(data, function(item) {
return { id: item.id, text: item.name };
});
$('#state').select2({ …Run Code Online (Sandbox Code Playgroud) 我想为 select2 创建一个自定义数据适配器,但我在网上看到的示例都使用了 AMD。我们不在我们的项目中使用 AMD。如何创建我的自定义 dataAdapter?实现current和query方法的普通对象是不够的。
我正在使用select2 v4.0.3和 JQuery。我将选择器初始化为:
$("#datatype").select2({
theme: "classic"
});
$("#unit").select2({
theme: "classic"
});
Run Code Online (Sandbox Code Playgroud)
然后我想检测一个变化#datatype并改变#unit我做的值如下:
$("#datatype").on('select2:select',function () {
$("#unit").val('21'); //udpate unit type to be 'none'
});
Run Code Online (Sandbox Code Playgroud)
问题是值 #unit设置为 21 但该选择器的显示标签没有更新。有什么建议 ?
似乎 select2 不知道该事件,或者尚未正确设置以侦听更改。我不确定解决方案是什么。我是网络技术的新手。