gpt4 book ai didi

javascript - 将文本字段添加到 html 下拉列表

转载 作者:搜寻专家 更新时间:2023-10-31 23:23:25 26 4
gpt4 key购买 nike

我有一个 html 下拉字段,其中应该在底部添加一个文本字段,命名为其他。在此字段中键入的文本应为 other:text

下拉 html 是:

<select name="drop">
<option selected="selected">Please select ...</option>
<option value="car">car</option>
<option value="bike">bike</option>
<option value="Other">Other</option>
</select>

这里 other 应该是文本字段。为了理解我保留了 other 作为选项,但我希望它是一个输入文本字段,其中值由用户给出。

提前致谢

最佳答案

您可以默认为其他文本添加隐藏的输入字段:

<input type='text' id='other' name='other' style="display:none"/>

并在您的 js 中捕获选择的更改,然后检查所选选项值是否等于 Other 并显示或隐藏该字段:

$('select').on('change', function(){
if($(this).val()=='Other'){
$('#other').show().focus();
}else{
$('#other').val('').hide();
}
})

希望这对您有所帮助。


$('select').on('change', function(){
if($(this).val()=='Other'){
$('#other').show().focus();
}else{
$('#other').val('').hide();
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="drop">
<option selected="selected">Please select ...</option>
<option value="car">car</option>
<option value="bike">bike</option>
<option value="Other">Other</option>
</select>
<input type='text' id='other' name='other' style="display:none" value='other:'/>

关于javascript - 将文本字段添加到 html 下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35771677/

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