在选择特定选项时,在下拉选择框中添加输入框。

4

当选中某个选项时,我需要在选择选项中添加输入框。每当用户选择“其他”选项时,将出现一个输入框供用户输入数据。

HTML:

<select>
  <option>Choose Your Name</option>
  <option>Frank</option>
  <option>George</option>
  <option>Other</option>
</select>

<!-- when other is selected add input
<label>Enter your Name
<input></input>
</label> -->

我的 jsfiddle 链接:http://jsfiddle.net/rynslmns/CxhGG/1/


你需要使用JavaScript或jQuery - 从这里开始:http://api.jquery.com/change/ - ntgCleaner
3个回答

10
您可以使用jQuery .change() 来绑定元素的变化事件。
试试这个: HTML
<select>
  <option>Choose Your Name</option>
  <option>Frank</option>
  <option>George</option>
  <option>Other</option>
</select>
<label style="display:none;">Enter your Name
<input></input>
</label>

Jquery

$('select').change(function(){
     if($('select option:selected').text() == "Other"){
        $('label').show();
     }
     else{
        $('label').hide();
     }
 });

在Fiddle中试试

更新:

您还可以动态添加一个输入框 -

HTML

<select>
  <option>Choose Your Name</option>
  <option>Frank</option>
  <option>George</option>
  <option>Other</option>
</select>

Jquery

$('select').change(function(){
   if($('select option:selected').text() == "Other"){
        $('html select').after("<label>Enter your Name<input></input></label>");
   }
   else{
        $('label').remove();
   }
});

在Fiddle中尝试


3

以下是纯Javascript版本,无需jQuery:

<script>
// Put this script in header or above select element
    function check(elem) {
        // use one of possible conditions
        // if (elem.value == 'Other')
        if (elem.selectedIndex == 3) {
            document.getElementById("other-div").style.display = 'block';
        } else {
            document.getElementById("other-div").style.display = 'none';
        }
    }
</script>

<select id="mySelect" onChange="check(this);">
        <option>Choose Your Name</option>
        <option>Frank</option>
        <option>George</option>
        <option>Other</option>
</select>
<div id="other-div" style="display:none;">
        <label>Enter your Name
        <input id="other-input"></input>
        </label>
</div>

jsFidle

如前所述,添加onChange事件,将其链接到函数并在此处理应该显示的内容等。


当我需要存储值时会发生什么?<input type="text" name="othername" /> 为了提交表单,名称在哪里指定?最好使用相同的“名称”,无论用户手动输入数据还是选择选项。 - Dre_Dre
你可以直接使用输入字段进行发布,基本上将名称添加到<input>中,并在check函数内更改其值以选择选项 - 或者如果选择了“其他”,则将其设置为空字符串。 - Goran.it

2

点击此处查看演示。

HTML:

<select id="choose">
    <option>Choose Your Name</option>
    <option>Frank</option>
    <option>George</option>
    <option value="other">Other</option>
</select>
<label id="otherName">Enter your Name
    <input type="text" name="othername" />
</label>

jQuery:

$(document).ready(function() {
    $("#choose").on("change", function() {
        if ($(this).val() === "other") {
            $("#otherName").show();
        }
        else {
            $("#otherName").hide();
        }
    });
});

请注意“其他”选项上的 value="other" 属性。这是脚本判断“其他”选项是否被选中的方式。
希望这能帮到你!

我本来也想发一个非常相似的答案。它在这个fiddle上:http://jsfiddle.net/rynslmns/CxhGG/1/ - Victoria Ruiz
你可以使用$("#choose").change(),因为它是$("#choose").on("change", http://api.jquery.com/change/)的快捷方式。 - mcolo
.change() 能用,没错,但无法扩展以适应事件命名空间,如果您选择实现它们的话。我想我也只是出于习惯使用 .on(),哈哈。 - theftprevention

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接