2013-10-09 45 views
0

如何添加数据库值到我的combox()在选定的索引更改事件另一个组合框在同一个jsp页面 请帮忙快............ ..在jsp中动态添加值到组合框

这里是我的代码

<tr> 
        <td>Item</td> 
        <td> 
         <select name="cboItems" id="cboItems"> 
          <option value="-1">--Select--</option> 
          <% 
           CommodityInfoActions comObj = new CommodityInfoActions(erpConn); 
           comObj.getAllRecords(); 
           Iterator itr = comObj.ListOfObjects.iterator(); 
           int i = 1; 
           while (itr.hasNext()) { 
            CommodityInfo newObj = (CommodityInfo) itr.next(); 
            String item = newObj.getCommodityName(); 
            long itemid = newObj.getId(); 
            out.println("<option value='" + itemid + "' >" + item + "</option>"); 
            i++; 
           } 

          %> 
         </select>         
        </td> 
       </tr> 
       <tr> 
        <td>Batch No. </td> 
        <td> 
         <select id="cboBatchNo"> 
          <option>--select--</option> 
          <% 
           try { 
            if (Long.parseLong(request.getParameter("cboItems")) > -1) { 
             CommodityPriceActions comp = new CommodityPriceActions(erpConn); 
             comp.getBatchno(Long.parseLong(request.getParameter("cboItems"))); 
             CommodityPrices comPrice = new CommodityPrices(); 
             itr = comp.ListOfObjects.iterator(); 
             i = 1; 
             while (itr.hasNext()) { 
              CommodityPrices newObj = (CommodityPrices) itr.next(); 

              out.println("<option value='" + newObj.getId() + "'>" + newObj.getBatchNo() + "</option>"); 
             } 
            } 
           } catch (Exception exc) { 
           } 
          %> 

         </select> 
        </td>       
       </tr> 

我想根据在运行时cboItems选择的选项OT改变cboBatchno的内容...

+0

您可以将onchange事件添加到combobox1并通过ajax发送请求,并将成功数据从ajax填充到combobox2中。 –

回答

0

@ ddMyth2857356我不知道你试过真的是?这里是暗示,

$('#combobox1').change(function(){ 
var id = $(this).val(); 
    $.ajax({ 
    url : "servlet_URL&id="+ id, 
    type : "POST", 
    success : function(data) { 
     $("#combobox2").html(data); 
    } 
}); 
}); 

这是一个想法。你想出了你所尝试的以及你面临的问题?希望这可以帮助。

+0

可以帮我解决你指定的数据部分问题。 – Myth

+0

@ ddMyth2857356您可以通过'requset.getparameter(“id”)''在servlet中接收id并使用该值查询表。最后使用jsp页面来编写你的数据,如''。然后你的jQuery Ajax会负责填充第二个组合框。 –

+0

Thaks为了您的帮助,我解决了我的问题 – Myth