selected.js :: 有人有实际的工作示例吗?

发布于 2024-12-06 18:44:23 字数 1074 浏览 5 评论 0原文

有人使用过并定制过一些基本的 selected.js 代码吗?

我已经下载了js、css和png,从示例中复制了一些代码,编写了我自己的超级简单示例,但我一定缺少一些东西。我已经验证了 code.jquery.js 已包含并已加载,与 selected.css 相同。

当我尝试打开一个极其简单的 SELECT 字段(下拉菜单)时,我得到一个非常小的字段,并且单击该字段不会执行任何操作。当我禁用 selected.js 时,我只需获取显示所有选项的 SELECT 即可。

以下是我在 jQuery 中添加简单 SELECT 的方法(我必须动态填充该字段,尽管在本例中它都是硬编码的):

    $html = '<select name="items" id="items" multiple="multiple" size="1" class="chosenElement">';
    $html += '<option value="foo">foo</option>';
    $html += '<option value="bar">bar</option>';
    $html += '<option value="baz">baz</option>';
    $html += '<option value="qux">qux</option>';    
    $html += '</select>';

然后,当我显示包含选项的模式对话框时,我调用:

$('.modal-body').html($html);
$('.chosenElement').chosen();

到目前为止,我修改和测试了各种排列,在谷歌上搜索解决方案或示例,但似乎没有任何效果。这可能是非常愚蠢的事情,比如某个地方缺少分号,但我在这个“10 分钟实施”上浪费了很多时间,所以我需要寻求一些帮助。

https://github.com/harvesthq/chosen

Has anyone used and customized some basic chosen.js code?

I have downloaded the js, css and png, copied some code from the examples, wrote my own super-simple example, but I must be missing something. I have verified that the code.jquery.js is included and gets loaded, same with the chosen.css.

When I try to bring up even an extremely simple SELECT field (drop-down), I get a very small field, and clicking the field does nothing. When I disable chosen.js, I simply get the SELECT with all the options displayed.

Here's how I add a simple SELECT within jQuery (I have to populate the field dynamically, although in this example it's all hard-coded):

    $html = '<select name="items" id="items" multiple="multiple" size="1" class="chosenElement">';
    $html += '<option value="foo">foo</option>';
    $html += '<option value="bar">bar</option>';
    $html += '<option value="baz">baz</option>';
    $html += '<option value="qux">qux</option>';    
    $html += '</select>';

Then, right when I display the modal dialog box containing the options, I call:

$('.modal-body').html($html);
$('.chosenElement').chosen();

So far, I have modified and tested all kinds of permutations, Googled for solutions or examples, but nothing seems to work. It's probably something very silly, like a missing semi-colon somewhere, but I've wasted so much time on this "10-minute implementation" that I need to ask for soem help.

https://github.com/harvesthq/chosen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

淡忘如思 2024-12-13 18:44:23

如果您确实想测试“最基本”的示例,我建议:

  1. 使用硬编码的 HTML(相对于动态添加的 html)
  2. select 元素中删除所有属性
  3. 仅将属性添加回一旦基本示例运行良好,就选择 select 元素。

请注意,select 元素上的 multiple="multiple" 属性确实使 chosen.js 的行为有所不同。

我在这里运行了您的代码: http://jsfiddle.net/99Dkm/1/

它有效很好。

我怀疑问题不在于 chosen.js 库,而在于你如何使用它(包装在一些基本的 jQuery onready 函数中缺失或其他)。

请注意,在 jsFiddle 的工作示例中,我仅包含 chosen.css & chosen.jquery.js

注意:从 http://cdnjs.com/libraries/chosen< 获取这些文件的 URL(javascript 和 css)< /a>

If you really want to test the "most basic" example, I'd suggest:

  1. Work on hardcoded HTML (vs dynamically added html)
  2. Remove all the attributes from the select element
  3. Only add back the attributes to the select element once a basic example runs fine.

Note that the multiple="multiple" attribute on the select element does make chosen.js behave differently.

I have ran your code here: http://jsfiddle.net/99Dkm/1/

And it works just fine.

I suspect the problem is not chosen.js library but rather how you use it (wrapping inside some basic jQuery onready function missing or else).

Notice that in my working examples on jsFiddle I only included chosen.css & chosen.jquery.js.

note: get the URLs for those files (javascript & css) from http://cdnjs.com/libraries/chosen

新人笑 2024-12-13 18:44:23

你必须瞄准选择

$('#items').chosen();

jsFiddle

you have to target the select

$('#items').chosen();

jsFiddle

滥情哥ㄟ 2024-12-13 18:44:23

当您动态填充字段时,JSON 结果集是否会带“文本”和“值”属性返回?如果不是,Chosen 将无法在其列表中正确格式化结果。事实上,它根本不会添加它们。我经历了惨痛的教训才知道这一点,因为我的结果最初是用“名称”和“ID”属性返回的。

When you populate the field dynamically, is the JSON result set coming back w/ "Text" and "Value" attributes? If it's not, Chosen will not format the results properly within its list. In fact, it won't add them at all. I learned this the hard way because my results were initially coming back w/ "Name" and "ID" attributes instead.

汹涌人海 2024-12-13 18:44:23

尝试从选择框中删除 size="1" 属性和/或设置具有更大宽度的样式属性。选择的生成元素的宽度基于底层选择框的宽度,因此如果您的选择框非常小,选择的选择也会如此。希望有帮助。

Try removing the size="1" attribute from the select box and/or setting a style attribute with a larger width. Chosen bases the width of the generated elements on the width of the underlying select box so if your select box is very small, the Chosen select will be as well. Hope that helps.

⊕婉儿 2024-12-13 18:44:23

遇到了类似的问题。我无法弄清楚在我的情况下这有效的原因是什么:

$j('select').livequery(
    function(){
        $j(this).chosen({width: "300"});
        $j('.search-field input').height(14);

    }, 
    function(){
         //remove event
    });

Ran into a similar problem. I wasn't able to figure out what was causing it by in my situation this worked:

$j('select').livequery(
    function(){
        $j(this).chosen({width: "300"});
        $j('.search-field input').height(14);

    }, 
    function(){
         //remove event
    });
萌辣 2024-12-13 18:44:23

将 jQuery 代码包装在里面:-

$(document).ready(function(){
  $('.chosenElement').chosen();
});

wrap the jQuery code inside :-

$(document).ready(function(){
  $('.chosenElement').chosen();
});
终陌 2024-12-13 18:44:23

有关所选选项的文档包括:

如果您的选择在实例化 Chosen 时隐藏,则必须指定
宽度或选择将以 0 的宽度显示。

。为了避免出现零宽度字段,您需要使用“宽度”选项,或者确保在调用 Chosen 时原始选择可见。

The documentation on Chosen options includes:

If your select is hidden when Chosen is instantiated, you must specify
a width or the select will show up with a width of 0.

To avoid a zero-width field appearing, you need to use the "width" option, or make sure your original Select is visible when you call Chosen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文