如何重新加载jquery dropkick对象

我使用一个简单的选择列表和jquery.dropkick库来使它美丽。 现在我想在更改相应的select元素后更改该dropkick内容(一个新选项进来)。 但只需调用$(’#select’)。dropkick(); 不起作用。

看起来它不受支持。 仅仅从头开始重建那个dropkick就足够了。 有没有可能“破坏”那个dropkick对象,然后通过调用.dropkick()方法重建它?

我遇到了同样的问题,无法找到解决方案,但最终成功地使这个黑客工作。

$select = $("#select1"); $select.removeData("dropkick"); $("#dk_container_select1").remove(); $select.append(""); $select.append(""); $select.dropkick(); 

我利用Diode的答案来修改Dropkick代码,使其更加清晰:

 // Reload the dropkick select widget after options have changed // usage: $("...").dropkick('reload'); methods.reload = function () { var $select = $(this); var data = $select.data('dropkick'); $select.removeData("dropkick"); $("#dk_container_"+ data.id).remove(); $select.dropkick(data.settings); }; 

在dropkick.js中的以下代码段后面添加上面的代码:

 methods.reset = function () { ... }; 

然后你可以以编程方式使用它:

  $('#my-select').html(""); $('#my-select').dropkick('reload'); 

对于更新的访客。 Dropkick使用重置方法…不重绘。

 $("select").dropkick('reset'); 

我遇到了同样的问题,我对jquery-dropkick-1.0.0脚本进行了一些修补以解决这个问题。 我在dropkick对象中添加了forceSyncWithSelect方法。

以下是我所做的更改列表:

  • 添加对标记的支持
  • 添加autoWidth设置以将宽度保留为css
  • 添加open,close,forceSyncWithSelect和isDropkicked的方法
  • 保持默认tabindex 0
  • 使用optgroup元素上的data-dkgroupclass属性添加optgroup类选项
  • 修复forceSyncWithSelect方法,如果值出现多次,则仅执行第一次出现
  • 切换点击菜单选项
  • 阻止IE关闭滚动条交互菜单(滚动条交互启动模糊事件,这种行为是错误的)

您可能不希望所有这些更改,但您可以制作差异补丁并采取您需要的东西(如果您让更改日志或将其来源添加到标题中会很高兴,我想向Jamie Lottering提出这些更改,但是我需要建立一个git hub帐户才能这样做。

 /** * DropKick * * Highly customizable  element that gets dropkicked lists = [], // Convenience keys for keyboard navigation keyMap = { 'left': 37, 'up': 38, 'right': 39, 'down': 40, 'enter': 13 }, // HTML template for the dropdowns dropdownTemplate = [ '
', '', '{{ label }}', '', '
', '
    ', '
', '
', '
' ].join(''), // HTML template for dropdown options optionTemplate = '
  • {{ text }}
  • ', optionGroupTemplate = '
  • {{ text }}
  • ', // Some nice default values defaults = { startSpeed: 1000, // I recommend a high value here, I feel it makes the changes less noticeable to the user theme: false, change: false, autoWidth: true }, // Make sure we only bind keydown on the document once keysBound = false ; // Called by using $('foo').dropkick(); methods.init = function (settings) { settings = $.extend({}, defaults, settings); return this.each(function () { var // The current list and place our new one in front of it $select.before($dk); // Update the reference to $dk $dk = $('#dk_container_' + id).fadeIn(settings.startSpeed); // Save the current theme theme = settings.theme ? settings.theme : 'default'; $dk.addClass('dk_theme_' + theme); data.theme = theme; // Save the updated $dk reference into our data object data.$dk = $dk; // Save the dropkick data onto the

    对于新访客。 Dropkick添加了重绘方法。

     $("select").dropkick('redraw'); 
     $('#select').html('').dropkick('refresh');