在选择时从数据库中获取值 在jsp中

问题描述 投票:2回答:5

我正在开发一个基于Web的应用程序,其中我有一个列表和一个依赖文本框。我在列表中添加了客户名称。当我在列表中选择客户名称时,我希望在文本框中填写客户ID。我尝试过使用javascript函数,但这只能帮助我了解客户名称。

客户ID不是选项标签的值,而是我在客户注册时输入的客户代码。所以我想从mysql数据库中获取客户代码。

任何人都可以给我一些建议吗?这是我的jsp代码。

        <%   DBConnection dbc=new DBConnection();   
             Connection con=dbc.getNewConnection();

             Statement st = null;
             ResultSet rs = null;

        try
        {
           st=con.createStatement() ;
           rs=st.executeQuery("select cname from CustomerMaster"); 
           %>
<td> 
   <select id="selectBox" >

         <%  while(rs.next()){ %>
                <option ><%= rs.getString(1)%></option>


      <%  } %>
javascript html mysql jsp
5个回答
2
投票

Priyanka Pawar,您需要在语句查询中获取customer-id,如下所示:

<%
    DBConnection dbc = new DBConnection();
    Connection con = dbc.getNewConnection();

    Statement st = null;
    ResultSet rs = null;

    try{
        st = con.createStatement();
        /* You need to get customerId here, suppose your cid is customerId from table */
        rs = st.executeQuery("select cid, cname from CustomerMaster"); 
%>

<td>
    <!-- Changing dropdown value will call javascript method populateCustomerId() -->
    <select id="selectBox" onchange="populateCustomerId();">
        <%while(rs.next()){ %>
            <!-- rs.getString(1) would be your customerId set as option value -->
            <option value="<%=rs.getString(1) %>"><%=rs.getString(2) %></option>
        <%} %>
    </select>
    <input id="customerId" type="text" value="" />
</td>

使用Javascript:

function populateCustomerId(){
    var selectBox = document.getElementById('selectBox');

    /* selected value of dropdown */
    var selectedCustomerId = selectBox.options[selectBox.selectedIndex].value;

    /* selected value set to input field */
    document.getElementById('customerId').value = selectedCustomerId; 
}

1
投票

如果2个客户名称相同怎么办?

 <select id="yourSelectId" name="nameSelect" >
  <option value="customerId">values</option>
   ......

通过访问select的id,使用javascript / jquery获取所选值

var customerId=document.getElementById("yourSelectId");

1
投票

尝试这样的事情:

<td class="cell9"><span style="color:#000000;">
<select id="mySelect" name="county"  onchange="myChangeFunction()">
<%
//Counties fetcher
Map<Integer, County> fetchedCounties=CountyFetcher.fetchCounties();

for(County county:fetchedCounties.values()){

String cName=county.getName();  

int id=county.getId();  

%>

<option value="<%=cName%>"><%=cName%></option>

<%}%>

</select></span></td>

0
投票

您可以将id放在选项值属性中,然后使用js填充文本框。

<option value="id">name</option>

0
投票
$(document).ajaxStop(function () {
    var importModel = window.localStorage.getItem('ImportModel');
    localStorage.removeItem('ImportModel');

    if (!importModel) {
        return;
    };

    importModel = JSON.parse(importModel);


    valueDateTextBox.val(importModel.valueDate);
    bookDateTextBox.val(importModel.bookDate);
    referenceNumberInputText.val(importModel.referenceNumber);
    costCenterSelect.val(importModel.costCenter.toString());

    $.each(importModel.table, function (i, v) {
        addRow(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9]);
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.