【问题标题】:fetch value from database on selection of <option> in jsp在 jsp 中选择 <option> 从数据库中获取值
【发布时间】:2015-02-18 03:00:05
【问题描述】:

我正在开发一个基于 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>


      <%  } %>

【问题讨论】:

  • 您想要一个 php 解决方案吗?
  • 我在 jsp/servlet Bolboa 中的项目

标签: javascript html mysql jsp


【解决方案1】:

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

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

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

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

【讨论】:

  • 是的,使用 javascript 我正在获取客户的姓名,但如何从 mysql 中检索该客户的 ID
  • select cname from CustomerMaster to select id, cname from CustomerMaster ..我希望id是列名..选项值中的rs.getInt(1)
【解决方案2】:

可以将id放在option value属性中,然后用js填充文本框。

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

【讨论】:

  • 我说的是客户 ID,在我的 mysql 数据库中我们可以说我在存储客户信息时输入的客户代码
  • 那你需要改变你的sql:rs=st.executeQuery("select id, cname from CustomerMaster")
  • 请看我编辑的问题。在列表中选择后,文本框将被填充。我只提供要列出的客户名称。
【解决方案3】:

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:

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

【讨论】:

  • @Priyanka Pawar:这应该满足你的要求。
  • 出色的工作。非常感谢 Prakash Kumar。
  • 嘿,在 mysql 数据库表客户名称值是客户代码而不是名称它的保存代码。
  • 这些代码是什么,可能是它的相关数据存储在其他一些表中。
  • 实际上客户代码是我的销售表中的外键。我想将客户代码和客户名称都存储在我的销售表中。当我的 servlet 收到请求时,它会存储客户代码而不是客户名称
【解决方案4】:

试试这样的:

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

【讨论】:

    【解决方案5】:
    $(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]);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      • 2019-09-30
      • 2017-10-21
      • 1970-01-01
      • 2015-10-19
      • 2012-12-26
      • 1970-01-01
      相关资源
      最近更新 更多