简体   繁体   English

如何插入可以将用户输入作为文本的选项

[英]How do I insert an option that can take user input as text

This is particularly basic, I'm a beginner and have recently gotten help from a very kind and sincere man that has gotten me to where I am now. 这是非常基本的,我是一个初学者,最近得到了一个非常善良和真诚的人的帮助,这使我到达了现在的位置。

I want to give the user a choice to either select one of the options provided or insert their own information. 我想给用户一个选择,要么选择提供的选项之一,要么插入他们自己的信息。

Heres what I've got so far: 这是到目前为止我得到的:

<div class=naviselection>
Start:
<select id='start' onchange="updateNavigationLink()">  
<option value="The International School of The Hague, Wijndaelerduin 1, 2554 BX Den Haag">School</option>
<option value="Centrum, Den Haag">City Center</option>

</select> End:
<select id='end' onchange="updateNavigationLink()">
<option value="The International School of The Hague, Wijndaelerduin 1, 2554 BX Den Haag">School</option>
 <option value="Centrum, Den Haag">City Center</option>

 </select>

 </div>

 <div class=Finallink>
 <span id="navigationLink"></span>

Now, I'm trying to allow the user to put in their own text (corresponding to an address) and have that be one of the options. 现在,我试图允许用户输入自己的文本(对应于地址)并将其作为选项之一。

Im thinking of having the text box appear once an option is clicked but I don't know how to define the value, I suppose it would be the result of some JavaScript… 我想让文本框在单击选项后出现,但我不知道如何定义该值,我想这可能是某些JavaScript的结果……

<option value=undefined>Your Location</option>

So what would the JavaScript be, how would I make the value of the option above what my user types in. 那么JavaScript是什么,我如何使该选项的值高于用户输入的值。

If anyone can help it would be very much appreciated, I've just only begun with javascript about a week ago and know the very basics but I'm learning... 如果有人能帮忙,将不胜感激,大约一个星期前,我才刚开始使用javascript,并且了解非常基础的知识,但是我正在学习...

It seems you are looking for datalist . 看来您正在寻找资料datalist Please, take a look at this answer . 请看看这个答案

You may want to have an input field right after the select box which is enabled/disabled depending on whether the selected option is "your location" (that should be the last option): 您可能希望在选择框后有一个输入字段,此框是否启用取决于您选择的选项是否为“您的位置”(应该是最后一个选项):

<input id="custom-address" disabled />

and in your javascript you need (I'm assuming that you're using jQuery) 并在您的JavaScript中需要(我假设您正在使用jQuery)

$("#start").change(function(){ //the following check will be triggered every time the selected option changes
    if ($('#start')[0].selectedIndex==$('#start').length) { //if the selected option is the last one
        $('#custom-address').prop("disabled", false); //enable the text input
    } else {
        $('#custom-address').prop("disabled", true); //otherwise disable it
    }
})

Hope this helps 希望这可以帮助

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM