selected.js :: 有人有实际的工作示例吗?
有人使用过并定制过一些基本的 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 分钟实施”上浪费了很多时间,所以我需要寻求一些帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您确实想测试“最基本”的示例,我建议:
select
元素中删除所有属性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:
select
elementselect
element once a basic example runs fine.Note that the
multiple="multiple"
attribute on theselect
element does makechosen.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
你必须瞄准选择
jsFiddle
you have to target the select
jsFiddle
当您动态填充字段时,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.
尝试从选择框中删除 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.
遇到了类似的问题。我无法弄清楚在我的情况下这有效的原因是什么:
Ran into a similar problem. I wasn't able to figure out what was causing it by in my situation this worked:
将 jQuery 代码包装在里面:-
wrap the jQuery code inside :-
有关所选选项的文档包括:
。为了避免出现零宽度字段,您需要使用“宽度”选项,或者确保在调用 Chosen 时原始选择可见。
The documentation on Chosen options includes:
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.