如何根据选择值显示表单输入字段?

iCo*_*ode 16 html javascript jquery jsp

我知道这很简单,我需要在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>
Run Code Online (Sandbox Code Playgroud)

现在我只想在用户选择其他时显示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>
Run Code Online (Sandbox Code Playgroud)

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

DVM*_*DVM 34

您的代码存在一些问题:

  1. 你缺少一个关于select元素id的开放引用,所以: <select name="dbType" id=dbType">

应该 <select name="dbType" id="dbType">

  1. $('this')应该是$(this):paranthesis中不需要引号.

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

  3. 当你初始化"选择"时,用它前面的var做它,除非你已经在函数的开始处完成它

试试这个:

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

http://jsfiddle.net/ks6cv/

用于交换机的更新:

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

更新 jQuery和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>??
Run Code Online (Sandbox Code Playgroud)


小智 5

演示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();
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)