简体   繁体   English

如何创建一个下拉菜单,以接受来自文本字段的输入并显示文本位置

[英]How to create a drop down that takes input from a text field and show the position on text

I am creating a drop down that takes input like a normal text field and show the position of the text. 我正在创建一个下拉菜单,接受像普通文本字段这样的输入并显示文本的位置。 For this I have created a drop down and a text field and hide the drop down under text field. 为此,我创建了一个下拉列表和一个文本字段,并在文本字段下隐藏了下拉列表。 Now I want to write a Jquery that will take input from the text field and matches it with drop down and if there is a match then show the option from the drop down. 现在,我想编写一个Jquery,它将从文本字段中获取输入并与下拉列表进行匹配,如果存在匹配项,则从下拉列表中显示选项。 Is it possible? 可能吗?

You can try with autocomplete Drop-down. 您可以尝试使用自动完成下拉菜单。 Refer this link: http://www.c-sharpcorner.com/UploadFile/ca3292/autocomplete-dropdown-using-jquery-with-C-Sharp/ 请参阅此链接: http : //www.c-sharpcorner.com/UploadFile/ca3292/autocomplete-dropdown-using-jquery-with-C-Sharp/

Check this example 检查这个例子

Js: JS:

var options=["First","Second"];
$("#data").on('keyup',function(){
    var v = $(this).val();
    $("#options").html("");
    $.each(options,function(index){
        if(options[index].indexOf(v) > -1){
            $("#options").append("<li>"+options[index]+"</li>");
        }
    })
})

**There are better and more efficient ways to do this you may use this as example to start with. **有更好和更有效的方法可以执行此操作,您可以以此为例开始。

This is your: 这是你的:

HTML HTML

Textbox: <input type="text" /><br />
Text and position: <span></span>
<select style="display: none;">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="vw">VW</option>
    <option value="audi" selected>Audi</option>
</select>

JQUERY JQUERY

$("input").on('change keyup paste', function(){
    var theInput = this;
    $.each($("select option"), function(index, option){
        if ($(option).val().toLowerCase().trim() == 
            $(theInput).val().toLowerCase().trim()){
            var textToShow = $(option).val().trim() + " " + Number(index + 1);
            $("span").text(textToShow);
            $("select").val($(theInput).val().toLowerCase().trim());
        } else {
            $("span").text("");
        }
    });
});

https://jsfiddle.net/L000qqr9/1/ https://jsfiddle.net/L000qqr9/1/

and here is another quick example as well 这也是另一个快速的例子

http://codepen.io/anon/pen/LVebbr http://codepen.io/anon/pen/LVebbr

 var $inputField = $('#inputText'), $dropdown = $('.dropdown'); $inputField.on('keyup', function(){ var _value = $(this).val(); $dropdown.children('li').each(function(){ ( $(this).html().indexOf(_value) > -1 ) ? $(this).css('opacity', '1') : $(this).css('opacity', '0'); }); }); 
 .dropdown li{ opacity:0 } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="text" id="inputText" /> <ul class="dropdown"> <li>op1</li> <li>op2</li> <li>op3</li> <li>op3</li> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将单词更改为文本字段或输入字段的下拉菜单? - How to change a word into drop down menu in text field or input field? 如何在文本字段中键入内容时显示下拉框 - How to show the drop down box when something is typed in the text field 从下拉列表中选择内容时如何显示隐藏文本字段。 - How to show hide a text field when something is selected from a drop down. “下拉列表”中的“文本输入”在Ajax Control Upload中被忽略,并且始终保持默认值 - Text Input from Drop Down list is ignored in Ajax Control Upload and takes default value all the time 如何从下拉菜单到文本字段获取内容? - How to get content from a drop down menu to text field? 如何使用下拉选项填写文本字段 - How to fill in a text field with drop down selection 如何从文本文件创建 html select 下拉列表 - How to create a html select drop down list from a text file 使用Javascript隐藏和显示下拉菜单和文本字段 - Using Javascript to hide and show drop down menu and text field 动态添加带有标签的输入文本字段代替下拉菜单 - Dynamically adding input text field with label in place of drop down menu 在产品页面上输入文本字段,而不是下拉列表中的组合 - Input text field instead of drop down for combinations on product page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM