0

我在这里使用jquery插件“双面多选”:http: //www.stevefenton.co.uk/cmsfiles/assets/File/twolateralmultiselect.html

使用以下代码使用常规 rails 助手成功显示常规多选框:

<%= f.select(:expertise1, Tag.all.collect {|tag| [ tag.value, tag.value ] }, {}, {multiple: true, class: "multiselect"}) %>

我在资产的 javascripts 文件夹中有 jquery.twolateralmultiselector.js 文件,并在页面源代码中查看它。必须加载 Jquery,因为它在所有地方都使用它(rails 3.2.8),并且在源代码中可见。(1.8.2)

但是,当我将此添加到 javascript 时(在 $(document).ready(function() 内):

    $(".multiselect").twosidedmultiselect();

我在 javascript 控制台中收到此错误,我完全不知道它的含义以及如何解决它:

Uncaught TypeError: Object [object Object] has no method 'twosidedmultiselect'

帮助!!非常感谢。

4

1 回答 1

-1

将您的 javascript 包含在DOM Ready 处理程序中,这应该可以解决问题

$(function() {
     // This is the only line you need for the plugin
     $(".multiselect").twosidedmultiselect();

    // The below script is for information / example...

    // How to get the current selected items (note, they won't always have 
    // a "selected" attribute, but they will be in the box with the original ID:

    var selectedOptions = $("#yourselect")[0].options;

});

您似乎在元素完全加载之前使用了插件。在 DOM 处理程序中编写代码应该可以解决您的问题

于 2012-10-31T21:24:42.137 回答