问题描述
我正在使用选择的插件来构建多个选择输入字段.在此处查看示例: http://harvesthq.github.io/chosen/#multiple-select
I'm using the chosen plugin to build multiple select input fields. See an example here: http://harvesthq.github.io/chosen/#multiple-select
如果已选择该选项,则默认行为会禁用该选项.在上面的示例中,如果您选择阿富汗",则其在下拉菜单中将显示为灰色,从而使您无法再次选择它.
The default behavior disables an option if it has already been selected. In the example above, if you were to select "Afghanistan", it would be greyed out in the drop-down menu, thus disallowing you from selecting it a second time.
我需要能够多次选择相同的选项.我可以添加插件或手动替代中的任何设置以允许这样做吗?
I need to be able to select the same option more than once. Is there any setting in the plugin or manual override I can add that will allow for this?
推荐答案
我创建了selected的版本,该版本允许您多次选择相同的项目,甚至将这些多个条目作为POST变量发送到服务器.这是您可以做到的方式(我认为这很容易):
I created a version of chosen that allows you to select the same item multiple times, and even sends those multiple entries to the server as POST variables. Here's how you can do it (fairly easily, I think):
(提示:在selected.jquery.js中使用搜索功能查找这些行)
更改:
this.is_multiple = this.form_field.multiple;
收件人:
this.is_multiple = this.form_field.multiple;
this.allows_duplicates = this.options.allow_duplicates;
更改:
Change:
classes.push("result-selected");
收件人:
if (this.allows_duplicates) {
classes.push("active-result");
} else {
classes.push("result-selected");
}
更改:
Change:
this.form_field.options[item.options_index].selected = true;
收件人:
if (this.allows_duplicates && this.form_field.options[item.options_index].selected == true) {
$('<input>').attr({type:'hidden',name:this.form_field.name,value:this.form_field.options[item.options_index].value}).appendTo($(this.form_field).parent());
} else {
this.form_field.options[item.options_index].selected = true;
}
然后,在调用chosen()
时,请确保包括allows_duplicates
选项:
Then, when calling chosen()
, make sure to include the allows_duplicates
option:
$("mySelect").chosen({allow_duplicates: true})
这篇关于jQuery选择插件.选择多个相同的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!