gpt4 book ai didi

javascript - Jquery 自动完成与组合框上的多个选择

转载 作者:行者123 更新时间:2023-11-28 05:12:15 25 4
gpt4 key购买 nike

我有一些现有的 jquery 自动完成代码,它具有我需要的所有功能。自动完成功能会在组合框中搜索以第一个字符开头的匹配项。我需要做的就是添加多重选择功能,如下所示:

jqueryui.com/autocomplete/#multiple

这是我的代码:

<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<meta name="HandheldFriendly" content="True">
<meta name="PalmComputingPlatform" content="True">
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<title>WebFOCUS Report</title>
</head>
<body>
<br />
<br />
<br />
<div>
Please choose an option :
<select size="3" id="decision2">
<option selected>1521</option>
<option>3336</option>
<option>5454</option>
<option>7890</option>
<option>2178</option>
</select>
</div>

<style>
.custom-combobox {
position: relative;
display: inline-block;
}
.custom-combobox-toggle {
position: absolute;
top: 0;
bottom: 0;
margin-left: -1px;
padding: 0;
}
.custom-combobox-input {
margin: 0;
padding: 5px 10px;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$.widget( "custom.combobox", {
_create: function() {
this.wrapper = $( "<span>" )
.addClass( "custom-combobox" )
.insertAfter( this.element );

this.element.hide();
this._createAutocomplete();
this._createShowAllButton();
},

_createAutocomplete: function() {
var selected = this.element.children( ":selected" ),
value = selected.val() ? selected.text() : "";

this.input = $( "<input>" )
.appendTo( this.wrapper )
.val( value )
.attr( "title", "" )
.addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
.autocomplete({
delay: 0,
minLength: 0,
source: $.proxy( this, "_source" )
})
.tooltip({
classes: {
"ui-tooltip": "ui-state-highlight"
}
});

this._on( this.input, {
autocompleteselect: function( event, ui ) {
ui.item.option.selected = true;
this._trigger( "select", event, {
item: ui.item.option
});
},

autocompletechange: "_removeIfInvalid"
});
},

_createShowAllButton: function() {
var input = this.input,
wasOpen = false;

$( "<a>" )
.attr( "tabIndex", -1 )
.attr( "title", "Show All Items" )
.tooltip()
.appendTo( this.wrapper )
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass( "ui-corner-all" )
.addClass( "custom-combobox-toggle ui-corner-right" )
.on( "mousedown", function() {
wasOpen = input.autocomplete( "widget" ).is( ":visible" );
})
.on( "click", function() {
input.trigger( "focus" );

// Close if already visible
if ( wasOpen ) {
return;
}

// Pass empty string as value to search for, displaying all results
input.autocomplete( "search", "" );
});
},

_source: function( request, response ) {
var matcher = new RegExp("\\b" + $.ui.autocomplete.escapeRegex(request.term), "i" );
response( this.element.children( "option" ).map(function() {
var text = $( this ).text();
if ( this.value && ( !request.term || matcher.test(text) ) )
return {
label: text,
value: text,
option: this
};
}) );
},

_removeIfInvalid: function( event, ui ) {

// Selected an item, nothing to do
if ( ui.item ) {
return;
}

// Search for a match (case-insensitive)
var value = this.input.val(),
valueLowerCase = value.toLowerCase(),
valid = false;
this.element.children( "option" ).each(function() {
if ( $( this ).text().toLowerCase() === valueLowerCase ) {
this.selected = valid = true;
return false;
}
});

// Found a match, nothing to do
if ( valid ) {
return;
}

// Remove invalid value
this.input
.val( "" )
.attr( "title", value + " didn't match any item" )
.tooltip( "open" );
this.element.val( "" );
this._delay(function() {
this.input.tooltip( "close" ).attr( "title", "" );
}, 2500 );
this.input.autocomplete( "instance" ).term = "";
},

_destroy: function() {
this.wrapper.remove();
this.element.show();
}
});

$( "#decision2" ).combobox();
$( "#decision2" ).on( "click", function() {
$( "#decision2" ).toggle();
});
} );
</script>
</body>
</html>

最佳答案

我修改了你的代码。请检查并告诉我这是否是您想要的。

<html>

<head>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<meta name="HandheldFriendly" content="True">
<meta name="PalmComputingPlatform" content="True">
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<title>WebFOCUS Report</title>
</head>

<body>
<br />
<br />
<br />
<div>
Please choose an option :
<select size="3" id="decision2">
<option selected>1521</option>
<option>3336</option>
<option>5454</option>
<option>7890</option>
<option>2178</option>
</select>
</div>

<style>
.custom-combobox {
position: relative;
display: inline-block;
}

.custom-combobox-toggle {
position: absolute;
top: 0;
bottom: 0;
margin-left: -1px;
padding: 0;
}

.custom-combobox-input {
margin: 0;
padding: 5px 10px;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(function() {
$.widget("custom.combobox", {
_create: function() {
this.wrapper = $("<span>")
.addClass("custom-combobox")
.insertAfter(this.element);

this.element.hide();
this._createAutocomplete();
this._createShowAllButton();
},

_createAutocomplete: function() {
var sourceArray = [];
var selectItems = this.element.context.children;
$.each(selectItems, function(i, item) {
sourceArray.push(item.value);
});

var selected = this.element.children(":selected"),
value = selected.val() ? selected.text() : "";

function split(val) {
return val.split(/,\s*/);
}

function extractLast(term) {
return split(term).pop();
}

this.input = $("<input>")
.appendTo(this.wrapper)
.val(value)
.attr("title", "")
.addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left")
.autocomplete({
delay: 0,
minLength: 0,
source: function(request, response) {
// delegate back to autocomplete, but extract the last term
var term = extractLast(request.term),
matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(term), "i");
response($.grep(sourceArray, function(item) {
return matcher.test(item);
}));
},
select: function(event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
})
.tooltip({
classes: {
"ui-tooltip": "ui-state-highlight"
}
});
},

_createShowAllButton: function() {
var input = this.input,
wasOpen = false;

$("<a>")
.attr("tabIndex", -1)
.attr("title", "Show All Items")
.tooltip()
.appendTo(this.wrapper)
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass("ui-corner-all")
.addClass("custom-combobox-toggle ui-corner-right")
.on("mousedown", function() {
wasOpen = input.autocomplete("widget").is(":visible");
})
.on("click", function() {
input.trigger("focus");

// Close if already visible
if (wasOpen) {
return;
}

// Pass empty string as value to search for, displaying all results
input.autocomplete("search", "");
});
},

_removeIfInvalid: function(event, ui) {

// Selected an item, nothing to do
if (ui.item) {
return;
}

// Search for a match (case-insensitive)
var value = this.input.val(),
valueLowerCase = value.toLowerCase(),
valid = false;
this.element.children("option").each(function() {
if ($(this).text().toLowerCase() === valueLowerCase) {
this.selected = valid = true;
return false;
}
});

// Found a match, nothing to do
if (valid) {
return;
}

// Remove invalid value
this.input
.val("")
.attr("title", value + " didn't match any item")
.tooltip("open");
this.element.val("");
this._delay(function() {
this.input.tooltip("close").attr("title", "");
}, 2500);
this.input.autocomplete("instance").term = "";
},

_destroy: function() {
this.wrapper.remove();
this.element.show();
}
});

$("#decision2").combobox();
$("#decision2").on("click", function() {
$("#decision2").toggle();
});
});
</script>
</body>

</html>

关于javascript - Jquery 自动完成与组合框上的多个选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41265234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com