【发布时间】:2014-09-04 06:39:09
【问题描述】:
是否可以为(选择)Chosen 插件输入自定义值,以创建自己的值作为选项?
【问题讨论】:
-
如果您的意思是要在所选插件中创建一个新的
option元素,请参见此处:stackoverflow.com/questions/11352207/…
标签: jquery jquery-chosen
是否可以为(选择)Chosen 插件输入自定义值,以创建自己的值作为选项?
【问题讨论】:
option 元素,请参见此处:stackoverflow.com/questions/11352207/…
标签: jquery jquery-chosen
是的,可以在 onClick 上调用此函数并在参数中传递自定义值
function customTags(tagName)
{
var customTagLength = $('li.search-choice').length; //Custom Option length -1 as compare to actual Option length
if(customTagLength>9)
{
return false;
}
// above code to limit select value in chosen
var html = '<option value="'+tagName+'">'+tagName+'</option>';
$('.chosen-select').append(html);
//This is what you need
var SearchChoiceArrayTagName = [tagName];
$('.chosen-select').find('option').filter(function (idx, option) {
if($.inArray(option.value, SearchChoiceArrayTagName) !== -1) {
return option;
}
}).prop('selected', 'true');
$('.chosen-select').trigger("chosen:updated");
$('.chosen-select').chosen().change(function(e, params){
if(params.deselected==tagName)
{
$(".chosen-select option[value='"+tagName+"']").remove();
$(".chosen-select").trigger('chosen:updated');
}
});
}
【讨论】: