gpt4 book ai didi

javascript - 在 jQuery 组合框上选择下拉项后触发事件

转载 作者:行者123 更新时间:2023-11-30 10:26:34 24 4
gpt4 key购买 nike

我正在使用 jQuery UI 库中的自动完成组合框来创建几个也接受下拉列表的文本字段——基本上是混合文本/下拉输入。我对其进行了自定义,因此它也接受自由文本输入,而不仅仅是下拉列表中的项目。

当用户从下拉列表中选择一个项目时,我想触发一个函数来根据输入填充表单的其余部分。我不希望在用户手动输入值时触发此功能。我不确定如何将事件专门绑定(bind)到从下拉列表中选择项目的用户。

这是一个 fiddle :http://jsfiddle.net/AhDHk/

HTML:

    <input type="text" name="realtor-name" id="lp-realtor-name" value="" class="text ui-widget-content ui-corner-all" />

JS:

// adds the dropdown, dropArray is the list of items for the dropdown, id is the ID of the html input. 

function textDropdown(dropArray, id) {
var $input = $(id).autocomplete({
source: dropArray, // this is an array fetched with AJAX of all the items for the dropdown
minLength: 0
}).addClass("ui-widget ui-widget-content ui-corner-left");


$("<button type='button'>&nbsp;</button>")
.attr("tabIndex", -1)
.attr("title", "Show All Items")
.insertAfter($input)
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass("ui-corner-all")
.addClass("ui-corner-right ui-button-icon lp-drop-button")
.click(function() {
// close if already visible
if ($input.autocomplete("widget").is(":visible")) {
$input.autocomplete( "close" );
return;
}
$(this).blur();
$input.autocomplete("search", "" );
$input.focus();
});

$("form").submit(function(e) {
event.preventDefault ? event.preventDefault() : event.returnValue = false;
alert($(this).serialize());
});
}

最佳答案

这里是你可以如何处理它:

var $input = $(id).autocomplete({
source: dropArray, // this is an array fetched with AJAX of all the items for the dropdown
minLength: 0,
select: function(event, ui) { alert('test');}
}).addClass("ui-widget ui-widget-content ui-corner-left");

可能重复:jQuery UI Autocompleteselect

关于javascript - 在 jQuery 组合框上选择下拉项后触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19337877/

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