在 JSP 中选择<选项>时从数据库中获取值
fetch value from database on selection of <option> in jsp
我正在开发一个基于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>
<% } %>
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;
}
如果 2 个客户具有相同的名称怎么办?
<select id="yourSelectId" name="nameSelect" >
<option value="customerId">values</option>
......
使用 JavaScript/jQuery 通过访问 select 的 ID 获取所选值
var customerId=document.getElementById("yourSelectId");
尝试这样的事情:
<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>
可以将 id 放在选项值属性中,然后使用 js 填充文本框。
<option value="id">name</option>
$(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]);
});
});
相关文章:
- 每隔5秒从数据库获取数据,并通过AJAX将其发送到视图
- 使用自动完成文本框从mysql数据库获取表记录并显示在文本字段中
- 尝试在移动优先应用程序中从 SQL 数据库获取数据时出错
- 如何在不刷新页面的情况下从 PHP 中的 SQL Server 数据库获取数据
- 如何根据屏幕尺寸从数据库获取数据
- 更新后,Laravel/Ajax从数据库获取值
- 从AngularJs中的数据库获取并显示数据
- 从sqlite数据库获取结果
- 如何使用ajax从数据库获取数据
- 滚动时从数据库获取图像 (HTML)
- 如何使从数据库获取的 Ruby 代码在 JS 中工作
- 从数据库获取所有纬度和长度以显示谷歌地图上的所有位置
- 如何从索引数据库获取所有值
- 通过 AJAX 从数据库获取 JSON
- 谁能告诉我如何使用 JavaScript 选择从数据库获取的所有项目到复选框中
- 从数据库获取数据后在谷歌地图上显示标记
- 是否可以使用 JavaScript 从 Django 模型数据库获取数据
- 使用 Ajax 从数据库获取数据
- 如何以 15 分钟的间隔在系统时间从数据库获取数据
- 从 MySQL 数据库获取数据时节点.js流不正确