jQuery select2不能在自定义模板中添加click事件
jQuery select2 cannot add click event to custom template
我试图将自定义单击事件添加到我嵌入在select2处理程序中的图标。我没有使用默认的"x",而是添加了三个象形图标,我想将自定义单击处理程序附加到它们上,并从那里开始使用select2事件API采取行动。
如果我点击实际的标签,它看起来会起作用,但不像我希望的那样在单个图标上。
我的JavaScript代码如下:
$( document ).ready( function() {
function formatPattern( p )
{
if (!p.id) return p.text;
var html;
html = '<div class="action-group">';
html += '<a href="#" class="glyphicon glyphicon-remove"></a>';
html += '<a href="#" class="glyphicon glyphicon-play"></a>';
html += '<a href="#" class="glyphicon glyphicon-pause"></a>';
html += '</div>';
html += '<div class="select-text">' + p.id + '</div>';
return html;
}
var tagBox = $( '#tagPicker' );
tagBox.select2({
tags: ['A', 'B', 'C'],
closeOnSelect: false,
formatResult: formatPattern,
formatSelection: formatPattern,
escapeMarkup: function(m) { return m; }
});
tagBox = tagBox.data( 'select2' );
tagBox.selectChoice = (function(fn) {
return function(data, options) {
var target;
console.log(data);
console.log(options);
if (options != null) {
target = $(options.target);
}
if (target && target.hasClass('glyphicon-play')) {
console.log(" clicked play button " );
} else {
return fn.apply(this, arguments);
}
}
})(tagBox.selectChoice);
});
这是jsfiddle: https://jsfiddle.net/Lwda44q5/
不幸的是,select2
只提供关于被单击的元素的信息,因为它不支持嵌套元素(li - ul以外的元素)。但是,您可以标记在选项中单击的元素(通过引入css
类或data-attribute
-留给您),然后在函数中找到该元素作为目标。
$( document ).ready( function() {
function formatPattern( p )
{
if (!p.id) return p.text;
var html;
html = '<div class="action-group">';
html += '<a href="#" class="glyphicon glyphicon-remove"></a>';
html += '<a href="#" class="glyphicon glyphicon-play"></a>';
html += '<a href="#" class="glyphicon glyphicon-pause"></a>';
html += '</div>';
html += '<div class="select-text">' + p.id + '</div>';
return html;
}
var tagBox = $( '#tagPicker' );
tagBox.select2({
tags: ['A', 'B', 'C'],
closeOnSelect: false,
formatResult: formatPattern,
formatSelection: formatPattern,
escapeMarkup: function(m) { return m; }
});
// introduce a click handler on the sub elements
$('.action-group a').on('click',function()
{
$('.action-group a').removeClass('active');
$(this).addClass('active');
})
tagBox = tagBox.data( 'select2' );
tagBox.selectChoice = (function(fn) {
return function(data, options) {
var target = $(data).find("a.active"); // find the active element
if (target && target.hasClass('glyphicon-play')) {
console.log(" clicked play button " );
} else {
return fn.apply(this, arguments);
}
}
})(tagBox.selectChoice);
});
示例:https://jsfiddle.net/Lwda44q5/1/
相关文章:
- 在控制器中添加$location依赖项时,ng-click停止激发
- 对于JQM中的外部链接,动态添加的链接上的触发器('click')不会传播
- 使用 ng-click 在 angularJs 中添加和删除类
- btAdd click事件在添加记录后打开两个窗口
- 如何使用 Easeljs 将 Click 事件添加到精灵表中
- 为什么 jquery .click() 在添加 [0] 时在 href 上工作
- 使 ng-click 函数通用以添加行
- 如何使用 xPath 添加 jQuery Click 事件
- 添加.(“click”)函数到新创建的按钮,事件在点击前触发
- 如何在jQuery中添加多个on('click')方法
- 如何在 ng-click 上为 AngularJS 添加类以查找多个元素(巧妙地)
- 遍历元素,向每个元素添加click事件
- 在窗口上添加click事件,但忽略所有以https开头的链接
- 在多个画布上添加Click事件处理程序
- jQuery select2不能在自定义模板中添加click事件
- 为多维数组中的元素添加Click Event
- 在javascript中为数组元素添加Click事件
- Jquery:动态地将li项附加到ul,然后添加click,但是click贯穿每个li
- 如何仅在设备处于x视图时添加click处理程序
- 如何使用javascript在表单元素上添加.click()事件