我有我的jsp文件,其中需要2个下拉菜单。如果我选​​择第一个下拉菜单,则基于值,它将显示另一个下拉菜单。
我在jsp中使用springMVC和jstl。

这是我的generate.jsp代码



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="sp" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Generation</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
    jQuery(document).ready(function(){
    $("#selectEmp").on('change',function(){

    	if(this.val =='2')
    	{
    		$("#dept").css('display','block');
    		$("#dept").show();
    	}
    	else
    	{
    		$("#dept").hide();
    	}
    });
});
</script>
</head>
<body>
<div class="dropdown generate-file">Type :<sp:select id="selectEmp" path="type" name="selectEmp">
				<option value="1">All</option>
  				<option value="2">Department</option>
  				<option value="3">Individual</option>
  				</sp:select></div><br/><br/>
  		<div class="dropdown generate-file" id="dept" style="display:none;">
  			Department:<sp:select  path=""><span class="caret"></span>
  				<option value="">R and D</option>
  				<option value="">Development</option>
  				<option value="">Sales</option>
  				<option value="">Admin</option>
  				<option value="">HR</option>
  				<option value="">Marketing</option>
  				<option value="">CRM</option>
  				<option value="">Finance</option>
  				</sp:select>
  		</div>
		</body>
</html>

最佳答案

您需要使用$(this).val() == '2'

$dept = $("#dept");
$(this).val() == '2' ? $dept.show() : $dept.hide();




jQuery(document).ready(function() {
  $("#selectEmp").on('change', function() {
    $dept = $("#dept");
    $(this).val() == '2' ? $dept.show() : $dept.hide();
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="dropdown generate-file">Type :
  <select id="selectEmp" name="selectEmp">
    <option value="1">All</option>
    <option value="2">Department</option>
    <option value="3">Individual</option>
  </select>
</div>
<br/>
<br/>
<div class="dropdown generate-file" id="dept" style="display:none;">
  Department:
  <select path=""><span class="caret"></span>
    <option value="">R and D</option>
    <option value="">Development</option>
    <option value="">Sales</option>
    <option value="">Admin</option>
    <option value="">HR</option>
    <option value="">Marketing</option>
    <option value="">CRM</option>
    <option value="">Finance</option>
    <select>
</div>

10-02 22:23