2017-11-10 160 views
0

所以,這就是:在我的JSP頁面上,我希望使用存儲過程填充數據庫的下拉列表,該存儲過程期望在兩個無線電中選擇的國家的id(即,如果你選擇美國,那個國家的國家就會被填補,如果你選擇尼加拉瓜,反之亦然)。正如我的問題所述,它沒有使用servlet,問題是我無法獲得所選無線電的value(如果我傳遞一個數字而不是變量「country」,則組合被填充)。Dropdown JSP數據庫存儲過程沒有servlet

這是我的代碼:

<label for="state">State</label> 
<select name="ddStates" id="ddStates"> 
    <option value="-1">Select State...</option>  
    <% 
    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/nextnetcustomers", "root", "root"); 
     int country = Integer.parseInt(request.getParameter("cntry")); 
     StringBuilder sb = new StringBuilder(); 
     ResultSet rs = null; 
     PreparedStatement ps = null; 
     sb.append("{call SP_CATSTATES_LIST(?)}"); 
     ps = conn.prepareCall(sb.toString()); 
     ps.setInt(1, country); 
     rs = ps.executeQuery(); 
     while (rs.next()) { 
      %> 
      <option value="<%=rs.getInt("IDCATSTATES")%>"><%=rs.getString("DESCRIPTION")%></option> 
      <% 
     } 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
     out.println("Error " + ex.getMessage()); 
    } 
    %> 
</select> 
+0

這是無線電裝置: 美國 < input type =「radio」name =「cntry」value =「2」/> NICARAGUA – Chris

回答

0

解決方案:好,最後我不得不使用與JSON數據和Ajax功能的一個servlet,如果別人面對這樣的問題,下面是一個簡單的解決方案:

AJAX的功能:

$(document).ready(function() { 
      $('#USA').change(function (event) { 
       var id = $("#USA").val(); 
       $.get('PopulateCustomersTable', { 
        idcontry: id 
       }, function (response) { 
        var select = $('#ddStates'); 
        select.find('option').remove(); 
        $.each(response, function (index, value) { 
         $('<option>').val(value.IdState).text(value.Description).appendTo(select); 
        }); 
       }); 
      }); 
      $('#NIC').change(function (event) { 
       var id = $("#NIC").val(); 
       $.get('PopulateCustomersTable', { 
        idcontry: id 
       }, function (response) { 
        var select = $('#ddStates'); 
        select.find('option').remove(); 
        $.each(response, function (index, value) { 
         $('<option>').val(value.IdState).text(value.Description).appendTo(select); 
        }); 
       }); 
      }); 
     }); 

無線電裝置:

<input type="radio" id="USA" name="cntry" value="1"/>USA 
<input type="radio" id="NIC" name="cntry" value="2"/>NICARAGUA 

和選擇下拉:

<select name="ddStates" id="ddStates" required> 
    <option value="">Select State...</option>              
</select> 
相關問題