允许使用 selected.js 多重选择添加新值
我正在使用 selected.js 插件 http://harvesthq.github.com/chosen/ 与jQuery 允许用户从一个选择中选择多个选项。然而,我现在希望能够让他们创建尚未存在的值 - 知道如何解决这个问题吗?
编辑:类似于SO自己的标签选择/创建栏的东西将接近我所追求的
最好不更改或编辑插件,但如果需要的话会这样做。
代码: HTML:
<p>Select something</p>
<select name="theSelect[]" multiple="multiple">
<option value="First Option">First Option</option>
<option value="Second Option">Second Option</option>
</select>
Javascript:
$(function(){
$('select').chosen();
});
因此,如果用户要输入“第三个选项”,我想将其添加到列表中并选择它。值和显示名称是/将是相同的,所以这不是问题
I'm using the chosen.js plugin http://harvesthq.github.com/chosen/ with jQuery to allow the user to select multiple options from a select. However, I now want to be able to let them create values that aren't already present - any idea how to go about this?
EDIT: something similar to SO's own tag selection/creation bar would be close to what I'm after
Preferably without changing or editing the plugin, but will do if required.
The code:
HTML:
<p>Select something</p>
<select name="theSelect[]" multiple="multiple">
<option value="First Option">First Option</option>
<option value="Second Option">Second Option</option>
</select>
Javascript:
$(function(){
$('select').chosen();
});
So if a user were to type in "Third Option", i'd like to add that to the list and have it selected. The value and display name are / will be the same so that's not a concern
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
根据文档,您可以尝试执行以下操作:
正如 Tony 在下面的评论中所述:
“从版本 1.0 开始,触发器现在“选择:更新”。请参阅 harvesthq.github.io/chosen/#change-update-events"
According to the documentation you can try doing something like this:
As Tony stated in the comments below:
"Starting with version 1.0 which the trigger is now "chosen:updated". See harvesthq.github.io/chosen/#change-update-events"
我偶然发现这个寻找相同的想法。
看起来这是一个非常受欢迎的功能请求,并且有几个分支已经实现了它。看起来它很快就会合并到主分支中。
+1对于这个特别有魅力的拉力:
https://github.com/harvesthq/chosen/pull/166
您可以在此处查看 Koenpunt 的分叉:https://github.com/koenpunt/chosen
I stumbled upon this looking for the same ideas.
Seems like its a pretty popular feature request, and a couple of forks have implemented it. Looks like it'll be merged into the master branch soon enough.
+1 for this particular pull which worked a charm:
https://github.com/harvesthq/chosen/pull/166
You can view Koenpunt's fork here: https://github.com/koenpunt/chosen
这是我做的一个简单的方法:
Here's a simple way that I did it:
我只是想解决同样的问题。我最终对源代码进行了一些修改。这是新的 keyup_checker 函数。看一下案例13:
I was just trying to solve the same problem. I wound up modifying the source code a bit. Here's the new keyup_checker function. Take a look at case 13:
我知道这不是答案,而是替代解决方案。
我正在搜索即时添加部分,发现 https://select2.org/tagging提供与选择相同的东西+其他东西,例如“标记”。
I know this not the answer, but an alternate solution.
I was searching for the on-the-fly adding part and found that https://select2.org/tagging provides the same thing as chosen + other stuffs like "Tagging".
您可以将一个事件附加到输入文本框来侦听特定的字符代码。之后添加选项并触发下拉列表的更新。
You could just attach an event to the input text box to listen for a particular character code. After that add the option and trigger the update on the dropdown.
对 leogdion 答案的更新,适用于选择的更高版本:
An update to leogdion's answer which works with later versions of chosen:
我再次更新了 3nochroot 的代码。现在我只有一个选择器来查找多选输入。
})
I have updated the code from 3nochroot once more. Now I have only one selector to find the multiselect input.
})
更新多项选择的 leogdion 答案 (gist)
Rails 上的示例用法 (slim)
Update of leogdion answer for multiple select (gist)
Example usage on rails (slim)
我尝试了这里和其他地方给出的几种解决方案,但是没有一个在 selected.js 1.8.5 (jQuery: 3.3.1) 中工作,所以我最终得到了以下结果,因为我不想使用可能并不总是启动的分叉- 到 master 分支的最新情况:
对于我可能不希望任何
.chosen-select
允许新值的情况,我添加了一个新类.chosen-newValuesAllowed
。我在此类上设置了一个事件处理程序,其中 CTRL + I 添加新值(如果尚不存在)。之后输入字段的焦点不会丢失。在我的示例中,我检查了 innerHTML,因为 @value 实际上包含数据库 ID,因此新值(在我的示例中是一个字符串,稍后将由服务器处理)永远无法在 @value 中找到。如果您想检查@value,请参阅代码片段内的评论。该代码处理单选和多选。
另一种解决方案是调用可触发函数 chosen:no_results 如果只有在明确没有结果时才应添加新值:
I tried several solutions given here and elsewhere, however none worked in chosen.js 1.8.5 (jQuery: 3.3.1) and so I ended up with the following since I didn't want to use a fork that might not always be up-to-date to the master branch:
For the case that I might not want any
.chosen-select
to allow new values, I added a new class.chosen-newValuesAllowed
. I set an event handler on this class where CTRL + I adds the new value if it is not yet present. The focus on the input field does not get lost afterwards. In my example, I check the innerHTML of the since @value actually contains database ids and therefore the new value, which is a string in my example that would be processed by the server later, could never be found in @value. If you want to check @value, please see the comment inside the snippet.The code handles single and multiple selects.
Another solution would be to call the triggerable function chosen:no_results if new values should only be added if there explicitly is no result: