【问题标题】:How to make one of the selected options in a drop-down list of an HTML form add a text input?如何使 HTML 表单的下拉列表中的选定选项之一添加文本输入?
【发布时间】:2016-01-30 04:53:55
【问题描述】:

我有以下代码:

<label for="city">City</label>
<select id="city" name="city" required>
  <option value="">Select city</option>
  <option value="city1">city1</option>
  <option value="city2">city2</option>
  <option value="other" onselect="">Other...</option>
</select>

当用户选择“其他”时,我希望表单在此下拉列表下方显示一个新的输入文本,以便他/她可以输入自己的城市。同样,如果用户在下拉列表中选择现有城市之一,则生成的文本输入将消失。

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

在选择后添加一个输入并使其显示为无

 <label for="city">City</label>
  <select id="city" name="city" required>
   <option value="">Select city</option>
   <option value="city1">city1</option>
   <option value="city2">city2</option>
   <option value="other" onselect="">Other...</option>
  </select>
 <input id="other" style="display:none" />

在脚本标签内

<script type="text/javascript">
$(document).ready(function () {

     $('#city').on('change', function (e) {

         var selectValue = this.value;
         if (selectValue == "other") {
             $("#other").show();
         }
         else {
             $("#other").hide();
         }

     });
 });
</script>

你还应该在你的 html 文件中在上面的脚本标签之前添加 Jquery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

结合 Html 和脚本后,您的 html 文件应如下所示

<html>
<body>

   <label for="city">City</label>
   <select id="city" name="city" required>
   <option value="">Select city</option>
   <option value="city1">city1</option>
   <option value="city2">city2</option>
  <option value="other" onselect="">Other...</option>
   </select>
  <input id="other" style="display:none" />

   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

 <script type="text/javascript">
 $(document).ready(function () {

 $('#city').on('change', function (e) {

     var selectValue = this.value;
     if (selectValue == "other") {
         $("#other").show();
     }
     else {
         $("#other").hide();
     }

 });
 });
</script>

</body>

</html>

【讨论】:

  • 'onselect' 中要写什么?还有如何定义脚本标签以及放置在哪里?
  • 没有什么要为“onselect”写的。你应该在你的html文件末尾添加脚本标签,就在
【解决方案2】:

Vadivel 的固定代码:

    <script type="text/javascript">
        $( document ).ready(function() {
            $('#city_value').attr('type','hidden');
            $('#city').change( function(){
                var city=$('#city').val();

                if(city=="other")
                {
                    $('#city_value').attr('type','text');
                }
                else
                {
                    $('#city_value').attr('type','hidden');

                }
            });
        });
    </script>

【讨论】:

    【解决方案3】:
    $('#city').live('change', function() {
       var selectedCity = $(this).val();
       if(selectedCity == "other"){
          $('#city').after('<input type="text" id="othercity" name="othercity">');
       }else{
          $('#othercity').remove():
       }
    });
    

    【讨论】:

    • 我将您的代码放在 之间,紧跟在 html 代码之后并运行它,但没有任何反应。感谢您的尝试!
    • 我假设你将它用于 $(document).ready 脚本,这对我来说很明显
    【解决方案4】:

    JavaScript 代码:

    function otherOptionCall()
    {
    var e = document.getElementById("city");
    var einput = document.getElementById("inputother");
    
    var strUser = e.options[e.selectedIndex].value;
    if(strUser == 2)
    {
      einput.style.display="block";
    }
    else
    {
     einput.style.display="none";
    }
    }
    

    HTML代码:

    <label for="city">City</label>
    <select id="city" name="city" required>
      <option value="">Select city</option>
      <option value="city1">city1</option>
      <option value="city2">city2</option>
      <option value="other" onselect="otherOptionCall()">Other...</option>
    </select>
    <input type="text" name="strother" id="inputother" style="display:none">
    

    【讨论】:

    • 我们可以很好地在js中调试,所以我更喜欢JS。
    【解决方案5】:

    使用此代码。

    这很好。

    您使用 Jquery 或 JavaScript 函数创建此类型

     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    

    <body>
    
    
        <div id=" " style="">
            <label for="city">City</label>
            <select id="city" name="city" required>
              <option value="">Select city</option>
              <option value="city1" onselect="city(this.val)">city1</option>
              <option value="city2" onselect="city(this.val)">city2</option>
              <option value="other" onselect="city(this.val)">Other...</option>
            </select>
            <input type="text" value="" id="city_value" placeholder="Enter City"/>
        </div>
    
        <script type="text/javascript">
            $( document ).ready(function() {
                     $('#city_value').hide();
                     $('#city').change( function(){
                            var city=$('#city').val();
                            if(city=="other")
                            {
    
                            $('#city_value').attr('required',true);
                            }
                            else
                            {
                            $('#city_value').attr('required',false);
    
                            }
                     });
                });
        </script>   </body>
    

    【讨论】:

    • 不,它没有。文本输入不会隐藏。感谢您的尝试!
    • 哦,好的。那么如何输入城市名称
    • 如果您提供您的联系信息,我可以在 Skype 或 Teamviewer 上向您展示
    • 我的联系邮箱:vadiveljobs@gmail.com 发送 Teamviewer 用户 ID 和密码
    • 几分钟前我刚刚给你发了邮件
    猜你喜欢
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 2019-09-24
    相关资源
    最近更新 更多