Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在下拉列表中添加新项_Javascript_Jquery_Html_Dropdownbox - Fatal编程技术网

Javascript 在下拉列表中添加新项

Javascript 在下拉列表中添加新项,javascript,jquery,html,dropdownbox,Javascript,Jquery,Html,Dropdownbox,如果在下拉列表中搜索数据时未找到数据,我想添加选项(添加新项目)。 我试了很多,但没有得到实际的结果 作为参考,你可以看到这个例子 (函数($){ //不区分大小写的jQuery:包含选择器 $.expr[“:”].searchableSelectContains=$.expr.createPseudo(函数(arg){ 返回函数(elem){ 返回$(elem).text().toUpperCase().indexOf(arg.toUpperCase())>=0; }; }); $.sear

如果在下拉列表中搜索数据时未找到数据,我想添加选项(添加新项目)。

我试了很多,但没有得到实际的结果

作为参考,你可以看到这个例子

(函数($){
//不区分大小写的jQuery:包含选择器
$.expr[“:”].searchableSelectContains=$.expr.createPseudo(函数(arg){
返回函数(elem){
返回$(elem).text().toUpperCase().indexOf(arg.toUpperCase())>=0;
};
});
$.searchableSelect=函数(元素、选项){
this.element=元素;
this.options=选项;
this.init();
var_this=这个;
此.searchableElement.click(函数(事件){
//event.stopPropagation();
_this.show();
}).on('keydown',函数(事件){
如果(event.which==13 | | event.which==40 | | event.which==38){
event.preventDefault();
_this.show();
}
});
$(文档)。在('click',null,函数(事件){
if(_this.searchablelement.has($(event.target)).length==0)
_this.hide();
});
this.input.on('keydown',函数(事件){
event.stopPropagation();
如果(event.which==13){//输入
event.preventDefault();
_选择currentHoverItem();
_this.hide();
}如果(event.which==27){//ese
_this.hide();
}如果(event.which==40){//down
_this.hoverNextItem();
}如果(event.which==38){//up
_this.hoverPreviousItem();
}
}).on('keyup',功能(事件){
if(event.which!=13&&event.which!=27&&event.which!=38&&event.which!=40)
_这个.filter();
})
}
var$sS=$.searchableSelect;
$sS.fn=$sS.prototype={
版本:“0.0.1”
};
$sS.fn.extend=$sS.extend=$.extend;
$sS.fn.extend({
init:function(){
var_this=这个;
this.element.hide();
this.searchableElement=$('');
该持有人=$('');
this.dropdown=$('');
this.input=$('');
this.items=$('');
this.caret=$('');
this.dropdown.append(this.input);
this.dropdown.append(this.items);
this.searchablelement.append(this.caret);
this.searchablelement.append(this.holder);
this.searchablelement.append(this.dropdown);
this.element.after(this.searchable元素);
this.buildItems();
},
过滤器:函数(){
var text=this.input.val();
this.items.find('.searchable select item').addClass('searchable-select-hide');
this.items.find('.searchable-select-item:searchable-selectcontains('+text+'))).removeClass('searchable-select-hide');
if(this.currentSelectedItem.hasClass('searchable-select-hide')&&this.items.find('.searchable-select-item:not(.searchable-select-hide)).length>0{
this.hoverFirstNotHideItem();
}
},
hoverFirstNotHideItem:函数(){
this.hoverItem(this.items.find('.searchable-select-item:not(.searchable-select-hide')).first();
},
selectCurrentHoverItem:function(){
if(!this.currentHoverItem.hasClass('searchable-select-hide'))
this.selectItem(this.currentHoverItem);
},
hoverPreviousItem:函数(){
如果(!this.hasCurrentHoverItem())
this.hoverFirstNotHideItem();
否则{
var previItem=this.currentHoverItem.prevAll('.searchable select item:not(.searchable select hide):first'))
如果(PreviItem.length>0)
本.悬停项(前一项);
}
},
hoverNextItem:function(){
如果(!this.hasCurrentHoverItem())
this.hoverFirstNotHideItem();
否则{
var nextItem=this.currentHoverItem.nextAll('.searchable-select-item:not(.searchable-select-hide):first')
如果(nextItem.length>0)
此.hoverItem(nextItem);
}
},
buildItems:function(){
var_this=这个;
this.element.find('option').each(function(){
var item=$(''+$(this.text()+'');
如果(选择此选项){
_此选项。选择项目(item);
_本项(项);
}
关于('mouseenter',function()的项{
$(this.addClass('hover');
}).on('mouseleave',function(){
$(this.removeClass('hover');
})。单击(功能(事件){
event.stopPropagation();
_this.selectItem($(this));
_this.hide();
});
_此.items.append(item);
});
},
show:function(){
this.dropdown.removeClass('searchable-select-hide');
this.input.focus();
this.status='show';
},
隐藏:函数(){
如果(!(this.status=='show'))
返回;
if(this.items.find(“:not(.searchable select hide)”).length==0)
这个.input.val(“”);
this.dropdown.addClass('searchable-select-hide');
this.searchablelement.trigger('focus');
this.status='hide';
},
hasCurrentSelectedItem:函数(){
返回this.currentSelectedItem&&this.currentSelectedItem.length>0;
},
选择项目:功能(项目){
if(this.hasCurrentSelectedItem())
this.currentSelectedItem.removeClass('selected');
this.currentSelectedItem=项目;
item.addClass('selected');
本项(项);
this.holder.text(item.text());
var值=项目数据(“值”);
此.持有者.数据('值',值);
this.element.val(值);
},
hasCurrentHoverItem:函数(){
返回this.currentHoverItem&&this.currentHoverItem.length>0;
},
hoverItem:函数(项){
if(this.hasCurrentHoverItem())
这个.currentHoverItem.removeClass('hover');
if(item.outerHeight()+item.position().top>this.items.height())
this.items.scrollTop(this.items.scrollTop()+item.outerHeight()+item.position().top-this.items.height());
否则如果(item.position().top<0)
this.items.scrollTop(this.items.scrollTop()+item.position().top);
this.currentHoverItem=项目;
item.addClass('hover'
$('#multipleSelect').selectivity({
           multiple: true,
           inputType: "Email",
           items: ["192.168.93.114:8181", "192.168.93.115:8181", "192.168.93.116:8181"],
           placeholder: 'Choose servers',
           showDropdown: true
    });
$("#multipleSelect").on("change", function(){
        var total =  $('#multipleSelect').selectivity("value");
        //Do something
    });
function AddItem(selectBox, name, value)
{
    var newOption = document.createElement("option");
    newOption.text = name;
    newOption.value = value;
    selectBox.add(newOption);
}