【发布时间】:2023-03-12 15:43:02
【问题描述】:
我正在尝试实现实时搜索。当我在文本字段中输入任何文本时,它应该与选项中可用的文本匹配。那么它应该在文本框下显示匹配的选项。 到目前为止,我已经尝试过了。 我所取得的是我得到了匹配的文本,我正在提醒它。但我希望它具有匹配结果的下拉列表。
代码
<select name="location_to" class="full-width">
<?php
foreach ( $flight_locations as $flight_location ) {
echo '<option value="' . esc_attr( $flight_location->term_id ) . '" ' . ' >' . esc_html( $flight_location->name ) . '</option>';
}
?>
</select>
**jquery code**
$('#location_from_input').keyup(function(){
var txt = $(this).val().toLowerCase();
// window.alert(txt);
// lenght = this.value.length;
$('#location_from>option').each(function () {
var text = $(this).text();
textL = text.toLowerCase();
// window.alert(txt);
(textL.indexOf(txt) == 0) ? window.alert(text) : $(this).hide();
});
})
我需要你的帮助。谢谢我提前。
【问题讨论】:
-
如果您只需要显示值匹配的选项,请将
window.alert(text)替换为$(this).show()。
标签: jquery