2017-11-10 1 views
0

これは次のとおりです。私のJSPページでは、パラメータとして2つのラジオで選択された国のidが必要なストアドプロシージャを使用してデータベースからドロップダウンを埋めたいあなたがアメリカを選択した場合、その国の国は満たされ、ニカラグアを選択した場合はその逆です)。私の質問によると、それはサーブレットを使用していない、問題は私がラジオのvalueを選択することができないということです(変数 "国"の代わりに番号を渡すと、コンボが満たされます)。サーブレットなしのドロップダウンJSPデータベースストアドプロシージャ

これは私のコードです:

<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

これはラジオです: の NICARAGUA – Chris

答えて

0

ソリューション:

:まあ、最終的に私は他の誰かがこの問題に直面した場合、JSONデータとAjax機能をサーブレットを使用していた、次はシンプルなソリューションです

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> 
関連する問題