【问题标题】:How to show form input fields based on select value?如何根据选择值显示表单输入字段?
【发布时间】:2013-03-12 02:41:43
【问题描述】:

我知道这很简单,我需要在 Google 中搜索。我尽了最大努力,但找不到更好的解决方案。我有一个表单字段,它需要一些输入和一个选择字段,它有一些值。它还具有“其他”值。

我想要的是:

如果用户选择“其他”选项,则应显示一个指定“其他”的文本字段。当用户选择另一个选项(不是“其他”)时,我想再次隐藏它。如何使用 JQuery 执行该操作?

这是我的 JSP 代码

<label for="db">Choose type</label>
<select name="dbType" id=dbType">
   <option>Choose Database Type</option>
   <option value="oracle">Oracle</option>
   <option value="mssql">MS SQL</option>
   <option value="mysql">MySQL</option>
   <option value="other">Other</option>
</select>

<div id="otherType" style="display:none;">
  <label for="specify">Specify</label>
  <input type="text" name="specify" placeholder="Specify Databse Type"/>
</div>

现在我只想在用户选择“其他”时显示 DIV 标记**(id="otherType")**。 我想试试 JQuery。这是我试过的代码

<script type="text/javascript"
    src="jquery-ui-1.10.0/tests/jquery-1.9.0.js"></script>
<script src="jquery-ui-1.10.0/ui/jquery-ui.js"></script>
<script>    
$('#dbType').change(function(){

   selection = $('this').value();
   switch(selection)
   {
       case 'other':
           $('#otherType').show();
           break;
       case 'default':
           $('#otherType').hide();
           break;
   }
});
</script>

但我无法得到这个。我该怎么办?谢谢

【问题讨论】:

标签: javascript jquery html jsp


【解决方案1】:

您的代码存在一些问题:

  1. 您在选择元素的 id 上缺少一个开放引号,因此:&lt;select name="dbType" id=dbType"&gt;

应该是&lt;select name="dbType" id="dbType"&gt;

  1. $('this') 应该是$(this):括号内不需要引号。

  2. 当您想要检索选项的值时,请使用 .val() 而不是 .value()

  3. 当你初始化“选择”时,在它前面加上一个 var,除非你在函数开始时已经这样做了

试试这个:

   $('#dbType').on('change',function(){
        if( $(this).val()==="other"){
        $("#otherType").show()
        }
        else{
        $("#otherType").hide()
        }
    });

http://jsfiddle.net/ks6cv/

UPDATE 用于开关:

$('#dbType').on('change',function(){
     var selection = $(this).val();
    switch(selection){
    case "other":
    $("#otherType").show()
   break;
    default:
    $("#otherType").hide()
    }
});

更新,带有 jQ​​uery 和 jQuery-UI 的链接:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>‌​

【讨论】:

  • 我仍然无法获得。我在问,这个问题是否与此有关? &lt;script type="text/javascript" src="jquery-ui-1.10.0/tests/jquery-1.9.0.js"&gt;&lt;/script&gt; &lt;script src="jquery-ui-1.10.0/ui/jquery-ui.js"&gt;&lt;/script&gt; &lt;script&gt;可以给在线JQuery链接吗?我需要检查它是否有效。谢谢
【解决方案2】:

Demo on JSFiddle

$(document).ready(function () {
    toggleFields(); // call this first so we start out with the correct visibility depending on the selected form values
    // this will call our toggleFields function every time the selection value of our other field changes
    $("#dbType").change(function () {
        toggleFields();
    });

});
// this toggles the visibility of other server
function toggleFields() {
    if ($("#dbType").val() === "other")
        $("#otherServer").show();
    else
        $("#otherServer").hide();
}

HTML:

    <p>Choose type</p>
    <p>Server:
        <select id="dbType" name="dbType">
          <option>Choose Database Type</option>
          <option value="oracle">Oracle</option>
          <option value="mssql">MS SQL</option>
          <option value="mysql">MySQL</option>
          <option value="other">Other</option>
        </select>
    </p>
    <div id="otherServer">
        <p>Server:
            <input type="text" name="server_name" />
        </p>
        <p>Port:
            <input type="text" name="port_no" />
        </p>
    </div>
    <p align="center">
        <input type="submit" value="Submit!" />
    </p>

【讨论】:

    【解决方案3】:

    您必须使用val() 而不是value(),并且您错过了起始报价id=dbType" 应该是id="dbType"

    Live Demo

    改变

    selection = $('this').value();
    

    到

    selection = $(this).val();
    

    或

    selection = this.value;
    

    【讨论】:

      【解决方案4】:

      我得到了答案。这是我的代码

      <label for="db">Choose type</label>
      <select name="dbType" id=dbType">
         <option>Choose Database Type</option>
         <option value="oracle">Oracle</option>
         <option value="mssql">MS SQL</option>
         <option value="mysql">MySQL</option>
         <option value="other">Other</option>
      </select>
      
      <div id="other" class="selectDBType" style="display:none;">
      <label for="specify">Specify</label>
      <input type="text" name="specify" placeholder="Specify Databse Type"/>
      </div>
      

      我的脚本是

      $(function() {
      
              $('#dbType').change(function() {
                  $('.selectDBType').slideUp("slow");
                  $('#' + $(this).val()).slideDown("slow");
              });
          });
      

      【讨论】:

        【解决方案5】:
        <!DOCTYPE html>
        <html>
        <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        
        <script>
        function myfun(){
        $(document).ready(function(){
        
            $("#select").click(
            function(){
            var data=$("#select").val();
                $("#disp").val(data);
                             });
        });
        }
        </script>
        </head>
        <body>
        
        <p>id <input type="text" name="user" id="disp"></p>
        
        <select id="select" onclick="myfun()">
        <option name="1"value="1">first</option>
        <option name="2"value="2">second</option>
        </select>
        
        </body>
        </html>
        

        【讨论】:

          【解决方案6】:
          $('#dbType').change(function(){
          
             var selection = $(this).val();
             if(selection == 'other')
             {
                $('#otherType').show();
             }
             else
             {
                $('#otherType').hide();
             } 
          
          });
          

          【讨论】:

            猜你喜欢
            • 2023-01-27
            • 2019-02-20
            • 1970-01-01
            • 2021-10-01
            • 2013-07-11
            • 2022-01-18
            • 1970-01-01
            • 2020-12-28
            • 1970-01-01
            相关资源
            最近更新 更多