javascript 在jsp中选择<option>时从数据库中获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27561568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:38:30  来源:igfitidea点击:

fetch value from database on selection of <option> in jsp

javascripthtmlmysqljsp

提问by Priyanka Pawar

I am developing one web based application where I have one list and one dependent textbox. I have added customer names in list. I want customer id to be filled in textbox when I will select customer name in list. I have tried using javascript function but that only helps me to know customer name.

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

Customer Id is not value of option tag its customer code which I have entered while registration of customer. So I want to fetch customer code from mysql database.

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

Can any one please suggest me something? This is my jsp code.

任何人都可以请给我一些建议吗?这是我的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>


      <%  } %>

采纳答案by Parkash Kumar

Priyanka Pawar, you need to fetch customer-id in statement query as following:

Priyanka Pawar,您需要在语句查询中获取客户 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:

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; 
}

回答by Danyal Sandeelo

What if 2 customers have the same names?

如果两个客户有相同的名字怎么办?

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

get the selected value using javascript/jquery by accessing the id of the select

通过访问选择的 id 使用 javascript/jquery 获取选定的值

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

回答by Mwangi Thiga

Try something like this:

尝试这样的事情:

<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>

回答by gfgfdg

$(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]);
    });
});

回答by bydsky

You can put the id in the option value property, then use js to fill the textbox.

你可以把id放在option value属性中,然后用js来填充textbox。

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