Javascript 选择<;选项>;在jsp中
我正在开发一个基于web的应用程序,其中我有一个列表和一个相关的文本框。我在列表中添加了客户名称。当我在列表中选择客户名称时,我希望在文本框中填写客户id。我尝试过使用javascript函数,但这只能帮助我了解客户名称 客户Id不是我在注册客户时输入的选项标签的值及其客户代码。所以我想从mysql数据库中获取客户代码 谁能给我提点建议吗? 这是我的jsp代码Javascript 选择<;选项>;在jsp中,javascript,html,mysql,jsp,Javascript,Html,Mysql,Jsp,我正在开发一个基于web的应用程序,其中我有一个列表和一个相关的文本框。我在列表中添加了客户名称。当我在列表中选择客户名称时,我希望在文本框中填写客户id。我尝试过使用javascript函数,但这只能帮助我了解客户名称 客户Id不是我在注册客户时输入的选项标签的值及其客户代码。所以我想从mysql数据库中获取客户代码 谁能给我提点建议吗? 这是我的jsp代码 <% DBConnection dbc=new DBConnection();
<% 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>
<% } %>
如果两个客户的名字相同怎么办
<select id="yourSelectId" name="nameSelect" >
<option value="customerId">values</option>
......
您可以将id放入option value属性中,然后使用js填充文本框
<option value="id">name</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>
试着这样做:
<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>
是的,使用javascript我获得了客户的名称,但如何从mysqlselect从CustomerMaster检索该客户的id以从CustomerMaster检索id,从CustomerMaster检索cname..我希望id是列名..rs.getInt(1)在选项的值中,我谈论的是客户id,在我的mysql数据库中,我们可以说是我在存储客户信息时输入的客户代码,然后您需要更改sql:rs=st.executeQuery(“从CustomerMaster选择id,cname”),请查看我编辑的问题。在列表中选择后,文本框将被填充。我只提供要列出的客户名称。您想要php解决方案吗?我的jsp/servlet项目Bolboa@Priyanka帕瓦尔:这应该能满足你的要求。它工作得很好。非常感谢Prakash Kumar。嘿,在mysql数据库表中,客户名称值是客户代码,而不是名称的保存代码。这些代码是什么,可能是存储在其他表中的相关数据。实际上,客户代码是我销售表中的外键。我想在我的销售表中存储客户代码和客户名称。当我的servlet获取请求时,它存储客户代码而不是客户名称
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;
}
<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>
$(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]);
});
});