简体   繁体   English

如何根据另一个下拉菜单中选择的值填充一个下拉菜单?

[英]How to populate a dropdown based on the value selected in another dropdown?

I have two dropdowns defined in JSP. 我在JSP中定义了两个下拉菜单。 The first dropdown fetches its values from the DB. 第一个下拉列表从数据库获取其值。 On selection of a value in this dropdown, the corresponding values for the second dropdown should be fetched from the DB depending on the first dropdown's selected value. 在此下拉菜单中选择一个值时,应根据第一个下拉菜单的选定值从数据库中获取第二个下拉菜单的相应值。 I have used Javascript and jquery to fetch the first dropdown's value. 我已经使用Javascript和jquery来获取第一个下拉列表的值。 But I'm unable to figure out how to reference this value back in the JSP to populate the second JSP. 但是我无法弄清楚如何在JSP中引用此值来填充第二个JSP。

This is my JSP code for the two dropdowns: 这是我的两个下拉菜单的JSP代码:

First Dropdown: 第一个下拉菜单:

<select name="dd1" id="dd1" style="width: 200px">
<option value="0">Choose New Salary</option>
     <%
     String Query1="select distinct Salary from TABLE1 where StartYear < 2005 order by Salary";
     Stmt1 = conn.createStatement();   
     ResultSet List1=Stmt1.executeQuery(Query1);

     while (List1.next())
     {
         int val1=List1.getInt("SALARY");
     %>

     <option value ="<%=val1%>"><%=val1%></option>
     <%            
      }
      %>                              

Second dropdown (I need to replace GETDD1DATA with the first dropdown's value): 第二个下拉菜单(我需要用第一个下拉菜单的值替换GETDD1DATA):

<select id="dd2" style="width: 200px">
 <option value="0">Choose New Position</option>
 <%
 String Query2="select distinct POSITION from TABLE1 where Salary=" + GETDD1DATA + " order by POSITION";
 Stmt2 = conn.createStatement();   
 ResultSet List2=Stmt2.executeQuery(Query2);
 while (List2.next())
 {
       int val2=List2.getInt("POSITION");
 %>

 <option value ="<%=val2%>"><%=val2%></option>
 <%            
     }
 %>                           

This is the Javascript/Jquery code to fetch the first dropdown's data (I'm able to see that the first dropdown's value is correctly stored in the variable selectedValue1): 这是用于提取第一个下拉列表的数据的Javascript / Jquery代码(我能够看到第一个下拉列表的值正确存储在变量selectedValue1中):

<script type="text/javascript">
   $(document).ready(function() {
     $("#dd1").change(function(){
      var selectedValue1 = $(this).val();
      //console.log(selectedValue1);
});
});
</script>

I need to pass the value of selectedValue1 from the Javascript to the JSP and reference it in the variable GETDD1DATA. 我需要将Javascript中的selectedValue1的值传递给JSP,并在变量GETDD1DATA中引用它。 I have explored session variables and hidden elements to store the dropdown1 value. 我探索了会话变量和隐藏元素来存储dropdown1值。 But these wouldn't work since the Java scriplet would be executed only once on page load when the session variable hasn't been declared yet in the Javascript. 但是这些都不起作用,因为当尚未在Javascript中声明会话变量时,Java脚本仅在页面加载时执行一次。 I also think reloading the entire page wouldn't be an option since the dropdown would be reset to its default value. 我还认为重新加载整个页面不是一种选择,因为下拉列表将重置为其默认值。 Kindly help me with solutions to my problem. 请帮助我解决问题。

You are not using the javascript code to populate the first dropdownlist. 您没有使用JavaScript代码填充第一个下拉列表。 You use javascript code to catch the event of selecting something in the first dropdownlist. 您使用JavaScript代码捕获在第一个下拉列表中选择某项的事件。

Also you cannot pass a value from javascript to JSP. 同样,您不能将值从javascript传递到JSP。 Javascript can interact with the HTML resulting from a JSP page. Javascript可以与JSP页面生成的HTML进行交互。

You seem not to have a clear concept of what a serverside language (in this case JSP) does and how it works. 您似乎对服务器端语言(在本例中为JSP)做什么以及如何工作没有一个清晰的概念。 I suggest you read http://www.javaworld.com/article/2076557/java-web-development/understanding-javaserver-pages-model-2-architecture.html . 我建议您阅读http://www.javaworld.com/article/2076557/java-web-development/understanding-javaserver-pages-model-2-architecture.html

So you can fill in the second form in two ways: Do a postback to the server passing the selected value as argument and use java or jsp to fill in the second dropdownlist. 因此,您可以通过两种方式填写第二种形式:将选择的值作为参数传递回服务器,并使用java或jsp填写第二个dropdownlist。 This will result in a complete page refresh each time you select another item in the first dropdownlist. 每次您在第一个下拉列表中选择另一个项目时,这将导致完整的页面刷新。

The second option (better) is to use ajax. 第二个选项(更好)是使用ajax。 This consists of two parts. 这包括两个部分。 Server: expose a webserivce that based on an argument (selected value from dd1) will retrieve the values for dd2. 服务器:公开一个基于参数(从dd1中选择的值)的webserivce,它将检索dd2的值。 Client: you allready have the event capture code in javascript. 客户端:您已经在javascript中有了事件捕获代码。 From javascript call the webservice with the selected value in dd1. 从javascript中,使用dd1中的选定值调用网络服务。 Parse the results and put them in the dd2 using javascript. 解析结果,然后使用javascript将其放入dd2中。

For better understanding look up serverside scripting, POSTBACK and ajax(the latter does not require postback and keeps your page responsive, this is the way to go) 为了更好地理解,请查找服务器端脚本,POSTBACK和ajax(后者不需要回发并保持页面响应速度,这是一种方法)

Please try with 请尝试

var x;

$('#pu-country').on('change', function () {
    if (this.value != '0') {

        $('#pu-city').prop('disabled', false);
        $('#pu-city').find("option").not(":first").remove();
        $('#pu-location').prop('disabled', true);
        $('#pu-location').val("Choose");

        switch (this.value) {
            case 'A':
                x = '<option value="A.1">A.1</option><option value="A.2">A.2</option><option value="A.3">A.3</option>'
        }
        $('#pu-city').append(x)
    } else {
        $('#pu-location').prop('disabled', true);
        $('#pu-location').val("Choose");
        $('#pu-city').prop('disabled', true);
        $('#pu-city').val("Choose");
    }


});

$('#pu-city').on('change', function () {
    if (this.value != '0') {

        $('#pu-location').prop('disabled', false);
        $('#pu-location').find("option").not(":first").remove();

        switch (this.value) {
            case 'A.1':
                x = '<option value="A.1.1">A.1.1</option><option value="A.1.2">A.1.2</option><option value="A.1.3">A.1.3</option>'
                break;
            case 'A.2':
                x = '<option value="A.2.1">A.2.1</option><option value="A.2.2">A.2.2</option><option value="A.2.3">A.2.3</option>'
                break;
            case 'A.3':
                x = '<option value="A.3.1">A.3.1</option><option value="A.3.2">A.3.2</option><option value="A.3.3">A.3.3</option>'
                break;

        }

        $('#pu-location').append(x)
    } else {
        $('#pu-location').prop('disabled', true);
        $('#pu-location').val("Choose");
    }


});

If you want to check the demo then please try here 如果您想查看演示,请尝试这里

Fiddler 提琴手

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM